regex to replace all tokens not in quotes?

QAZ

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. For example "ID1" becomes "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):

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.

Can anybody please advise how to process this correctly, thanks.

anubhava

You can use this regex based on alternation in String#replace with a callback function:

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'].

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

remove all commas between quotes with a vim regex

来自分类Dev

Vim find and replace all occurrences of a regex string

来自分类Dev

Rust regex replace_all比PHP regex preg_replace_callback慢,如何优化?

来自分类Dev

Going through all :tokens in a URL without the ":"

来自分类Dev

在Groovy中将特定字符串与replace all和regex匹配

来自分类Dev

在Rust中,当Rep包含一些变量时,如何做Regex的replace_all(regex,Rep)?

来自分类Dev

Ansible regex_replace

来自分类Dev

如何使用regex_replace?

来自分类Dev

Ruby regex search and replace array

来自分类Dev

How to add quotes for strings and null values between commas by using regex

来自分类Dev

如何用PHP regex_replace?

来自分类Dev

.net Regex.Replace中的错误?

来自分类Dev

Replace text using regex in RAD Studio

来自分类Dev

简单的preg_replace + RegEx的问题

来自分类Dev

js replace和Regex排除单词

来自分类Dev

std :: regex_replace期间堆栈溢出

来自分类Dev

带有模式变量的Regex.Replace()

来自分类Dev

在Powershell c#中用Regex替换-replace

来自分类Dev

停止Regex.Replace修改原始变量

来自分类Dev

Prestashop Smarty中的regex_replace问题

来自分类Dev

C#与Regex.Replace发生问题

来自分类Dev

半花式Regex.Replace()函数

来自分类Dev

C#Pig Latin with Regex Replace

来自分类Dev

regex find replace strings starting or beginning with backslash

来自分类Dev

std :: regex_replace期间堆栈溢出

来自分类Dev

Regex.Replace 没有正确替换

来自分类Dev

oracle sql中的regex_replace

来自分类Dev

Regex.Replace 意外行为

来自分类Dev

regex_replace 后的垃圾字符