使用带有javascript的正则表达式仅提取字符串的一部分

时间:2022-09-13 11:10:58

I'm trying to extract a part of string and replace it using regex in javascript. eg:

我正在尝试提取字符串的一部分,并在javascript中使用正则表达式替换它。例如:

var string = "application == xyz AND location == abc AND packet.vlan == 10 OR ip == 1.1.1.1" 

I need to replace

我需要更换

packet.vlan == 10 

with

packet.vlan == VLAN-10

I have tried the following

我尝试了以下内容

var regexp = /(\=.+)/g;
string.replace(regexp, ("==" + "VLAN-10");

selection has to stop at next OR/AND. In case of eg above selection has to stop before the start of string ip.

选择必须在下一个OR / AND停止。在例如上面的情况下,选择必须在字符串ip开始之前停止。

5 个解决方案

#1


1  

Your regex means "find any '=' sign followed by one or more characters."

你的正则表达式意味着“找到任何'='符号后跟一个或多个字符。”

You can have a look at https://regex101.com/ which provide visual ways to debug regex.

您可以查看https://regex101.com/,它提供了调试正则表达式的可视方法。

try string.replace(/(packet\.vlan == )(\d+)/, "$1VLAN-$2");

尝试string.replace(/(packet \ .vlan ==)(\ d +)/,“$ 1VLAN- $ 2”);

Note: "string" is a really bad name for a variable.

注意:“string”是变量的一个非常糟糕的名称。

#2


1  

You can use regex to replace the string, g identifier in RegExp is used as global match (find all matches rather than stopping after the first match).

您可以使用正则表达式替换字符串,RegExp中的g标识符用作全局匹配(查找所有匹配项而不是在第一次匹配后停止)。

var string = "application == xyz AND location == abc AND packet.vlan == 10 OR ip == 1.1.1.1"

var temp = new RegExp("packet.vlan == 10", "g");
console.log(string.replace(temp, "packet.vlan == VLAN-10"));

To replace the only first occurrence of packet.vlan == 10 with VLAN-10, you can simply use .replace().

要使用VLAN-10替换packet.vlan == 10中唯一的第一次出现,您只需使用.replace()即可。

var string = "application == xyz AND location == abc AND packet.vlan == 10 OR ip == 1.1.1.1 AND packet.vlan = 11.1.1.1.11"
console.log(string.replace("packet.vlan == 10", "packet.vlan == VLAN-10"));

#3


0  

Try like this,

试试这样,

const regex = /packet.vlan == (\d+)/g;
const str = `application == xyz AND location == abc AND packet.vlan == 10 OR ip == 1.1.1.1`;
const subst = `packet.vlan == VLAN-$1`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

See regex: https://regex101.com/r/93AfJr/1

请参阅regex:https://regex101.com/r/93AfJr/1

OR

const str = `application == xyz AND location == abc AND packet.vlan == 10 OR ip == 1.1.1.1`;
var newstr = str.replace(/packet.vlan == 10/i, 'packet.vlan == VLAN-10');
console.log(newstr);

#4


0  

The solution is to rewrite your regex:

解决方案是重写你的正则表达式:

var regexp = /(==\s)(.+\b)/g;  //all chars from ==whitespace until next word boundery
    var s = "packet.VLAN == 10".replace(regexp, "== VLAN-$2"); //$2 to refer to the capturing group
    console.log(s);

However with these complex sql strings I would use other JavaScript to ensure more control. Take a look:

但是对于这些复杂的sql字符串,我会使用其他JavaScript来确保更多控制。看一看:

var SQLString = "application == xyz AND location == abc AND packet.vlan == 10 OR ip == 1.1.1.1";


var edit = SQLString.split(/AND|OR/i); //split on and or
var andOrs = SQLString.match(/AND|OR/ig); //create an array with and an or.
edit.forEach(function(value, index, arr){
    //loop the array
    var prop = value.substr(0, value.trim().search(/\s/)+1).trim(); //trim the whitespaces.
    switch(prop){
       case "packet.vlan" :
           arr[index] = "packet.vlan == VLAN-" +value.split("==")[1].trim();
       break;
    }
    
    //add the original and or to it.
    andOrs[index] ? arr[index] = arr[index].trim() + " " + andOrs[index].trim() : null;
});

SQLString = edit.join(" "); //join the array parts with a whitespace.
console.log(SQLString);

On a final note. I'm curious why JavaScript is needed to rewrite SQL strings?

最后一点。我很好奇为什么需要JavaScript来重写SQL字符串?

#5


0  

Since i had to find the string which is packet.xxx and replace it with value. I had to loop through and find the string which consists packet. and replace it using regex.

因为我必须找到packet.xxx的字符串并用值替换它。我不得不循环查找包含数据包的字符串。并使用正则表达式替换它。

NOTE: In packet.xxx, xxx can be any dynamic value.

注意:在packet.xxx中,xxx可以是任何动态值。

#1


1  

Your regex means "find any '=' sign followed by one or more characters."

你的正则表达式意味着“找到任何'='符号后跟一个或多个字符。”

You can have a look at https://regex101.com/ which provide visual ways to debug regex.

您可以查看https://regex101.com/,它提供了调试正则表达式的可视方法。

try string.replace(/(packet\.vlan == )(\d+)/, "$1VLAN-$2");

尝试string.replace(/(packet \ .vlan ==)(\ d +)/,“$ 1VLAN- $ 2”);

Note: "string" is a really bad name for a variable.

注意:“string”是变量的一个非常糟糕的名称。

#2


1  

You can use regex to replace the string, g identifier in RegExp is used as global match (find all matches rather than stopping after the first match).

您可以使用正则表达式替换字符串,RegExp中的g标识符用作全局匹配(查找所有匹配项而不是在第一次匹配后停止)。

var string = "application == xyz AND location == abc AND packet.vlan == 10 OR ip == 1.1.1.1"

var temp = new RegExp("packet.vlan == 10", "g");
console.log(string.replace(temp, "packet.vlan == VLAN-10"));

To replace the only first occurrence of packet.vlan == 10 with VLAN-10, you can simply use .replace().

要使用VLAN-10替换packet.vlan == 10中唯一的第一次出现,您只需使用.replace()即可。

var string = "application == xyz AND location == abc AND packet.vlan == 10 OR ip == 1.1.1.1 AND packet.vlan = 11.1.1.1.11"
console.log(string.replace("packet.vlan == 10", "packet.vlan == VLAN-10"));

#3


0  

Try like this,

试试这样,

const regex = /packet.vlan == (\d+)/g;
const str = `application == xyz AND location == abc AND packet.vlan == 10 OR ip == 1.1.1.1`;
const subst = `packet.vlan == VLAN-$1`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

See regex: https://regex101.com/r/93AfJr/1

请参阅regex:https://regex101.com/r/93AfJr/1

OR

const str = `application == xyz AND location == abc AND packet.vlan == 10 OR ip == 1.1.1.1`;
var newstr = str.replace(/packet.vlan == 10/i, 'packet.vlan == VLAN-10');
console.log(newstr);

#4


0  

The solution is to rewrite your regex:

解决方案是重写你的正则表达式:

var regexp = /(==\s)(.+\b)/g;  //all chars from ==whitespace until next word boundery
    var s = "packet.VLAN == 10".replace(regexp, "== VLAN-$2"); //$2 to refer to the capturing group
    console.log(s);

However with these complex sql strings I would use other JavaScript to ensure more control. Take a look:

但是对于这些复杂的sql字符串,我会使用其他JavaScript来确保更多控制。看一看:

var SQLString = "application == xyz AND location == abc AND packet.vlan == 10 OR ip == 1.1.1.1";


var edit = SQLString.split(/AND|OR/i); //split on and or
var andOrs = SQLString.match(/AND|OR/ig); //create an array with and an or.
edit.forEach(function(value, index, arr){
    //loop the array
    var prop = value.substr(0, value.trim().search(/\s/)+1).trim(); //trim the whitespaces.
    switch(prop){
       case "packet.vlan" :
           arr[index] = "packet.vlan == VLAN-" +value.split("==")[1].trim();
       break;
    }
    
    //add the original and or to it.
    andOrs[index] ? arr[index] = arr[index].trim() + " " + andOrs[index].trim() : null;
});

SQLString = edit.join(" "); //join the array parts with a whitespace.
console.log(SQLString);

On a final note. I'm curious why JavaScript is needed to rewrite SQL strings?

最后一点。我很好奇为什么需要JavaScript来重写SQL字符串?

#5


0  

Since i had to find the string which is packet.xxx and replace it with value. I had to loop through and find the string which consists packet. and replace it using regex.

因为我必须找到packet.xxx的字符串并用值替换它。我不得不循环查找包含数据包的字符串。并使用正则表达式替换它。

NOTE: In packet.xxx, xxx can be any dynamic value.

注意:在packet.xxx中,xxx可以是任何动态值。