简体   繁体   English

正则表达式替换所有不在引号中的令牌?

[英]regex to replace all tokens not in quotes?

I am trying to process some input data in JavaScript whereby I need to replace the occurrences of all string tokens (in the form "ID1", "ID2", "ID3", ...) with a string that wraps the original token. 我试图在JavaScript中处理一些输入数据,我需要用包装原始令牌的字符串替换所有字符串标记(格式为“ID1”,“ID2”,“ID3”,...)的出现。 For example "ID1" becomes "table['ID1']". 例如,“ID1”变为“table ['ID1']”。 However if the original token is wrapped in quotes (single or double) it must be ignored. 但是,如果原始令牌包含在引号(单引号或双引号)中,则必须将其忽略。

For example the input string: 例如输入字符串:

var input = "ID10 \"ID0\" FOO 'ID0' #ID0# ID10 BAR ID1 ID0.";

should become: 应成为:

"table['ID10'] \"ID0\" FOO 'ID0' #table['ID0']# table['ID10'] BAR table['ID1'] table['ID0']."

I can currently get some of the way using the following code ( Try it on jsbin.com here ): 我现在可以使用以下代码获得一些方法( 在jsbin.com上试试 ):

var input = "ID10 \"ID0\" FOO 'ID0' #ID0# ID10 BAR ID1 ID0.";

var expected = "table['ID10'] \"ID0\" FOO 'ID0' #table['ID0']# table['ID10'] BAR table['ID1'] table['ID0'].";

// assume 15 is the max number of ids. we search backwards.
for( i=15 ; i>=0 ; i-- )
{
    var id = "ID" + i;

    var regex = new RegExp( "[^\"\']" + id + "", 'g' );

    input = input.replace( regex, "table['" + id + "']" );
}

if( input == expected )
    alert( 'success :)' );

This produces the output: 这会产生输出:

ID10 "ID0" FOO 'ID0' table['ID0']#table['ID10'] BARtable['ID1']table['ID0'].

It seems close to working, however the first id (ID10) gets ignored and the first character before a match gets lost. 它似乎接近工作,但第一个id(ID10)被忽略,匹配前的第一个字符丢失。

Can anybody please advise how to process this correctly, thanks. 任何人都可以建议如何正确处理,谢谢。

I think you're going to need a negative lookahead token. 我认为你需要一个负面的前瞻标记。

Take a look here 看看这里

The whole regex is 整个正则表达式是

(ID\d+(?!\\))

The negative lookahead is the (?!...) . 负向前瞻是(?!...) It just asserts that the next character after the digits is not a backslash 它只是断言数字后面的下一个字符不是反斜杠

So the code would be something along the lines of 所以代码就是这样的

var re = /(ID\d+(?!\\))/g; 
var str = 'ID10 \"ID0\" FOO \'ID0\' #ID0# ID10 BAR ID1 ID0.';
var subst = 'table[\'$1\']'; 
var result = str.replace(re, subst);
// table['ID10'] \"ID0\" FOO 'table['ID0']' #table['ID0']# table['ID10'] BAR table['ID1'] table['ID0'].

You can use this regex based on alternation in String#replace with a callback function: 您可以使用此正则表达式基于String#replace交替使用回调函数:

var input = "ID10 \"ID0\" FOO 'ID0' #ID0# ID10 BAR ID1 ID0.";
var r= input.replace(/"[^"]*"|'[^']*'|(ID\d+)/g, function($0, $1) {
       return ($1)? "table['"+$1+"']" : $0;});
//=> table['ID10'] "ID0" FOO 'ID0' #table['ID0']# table['ID10'] BAR table['ID1'] table['ID0'].

Edit it seems that zero-width negative look-behind is no supported in Javascript, so you need a zero-width negative look-ahead to check the next character after the ID plus digits is not either backslash, single or double quotes. 编辑似乎Javascript中不支持零宽度负面后视,因此在ID加数字不是反斜杠,单引号或双引号之后,您需要零宽度负前瞻以检查下一个字符。

you could try 你可以试试

/(ID\d+(?![\\\'\"]))/g

EDIT Forget all this! 编辑忘了这一切!

You need a zero-width negative look-behind 你需要一个零宽度的负面观察

you could try 你可以试试

 /(?<![\\"\\'])ID\\d+/g 

alternatively, you might try to capture your match in a group 或者,您可以尝试捕获组中的匹配项

 /[^\\"\\'](ID\\d+)/g 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM