简体   繁体   English

字符串内双引号的正则表达式

[英]Regex for double quotes inside string

A follow up to my previous question here .术后随访我刚才的问题在这里 I realize I need to be more specific regarding my regex case to get an answer that works for my case.我意识到我需要更具体地了解我的正则表达式案例以获得适用于我的案例的答案。

I am fighting with this regex already for quite some time (also using the answers from my previous question) and I seem to be unable to build what I need.我已经与这个正则表达式斗争了很长一段时间(也使用了我上一个问题的答案),但我似乎无法构建我需要的东西。

I need to replace all occurrences of two recurring single quotes '' with \\' inside all strings (so inside strings means between single quotes 'somesStringWithOrWithoutDoubleQuoteOccurrences' ).我需要在所有字符串中用\\'替换所有出现的两个重复单引号'' (所以在字符串内部意味着在单引号'somesStringWithOrWithoutDoubleQuoteOccurrences' )。 It is because in one language (syntax) quotes within the string are escaped with a ' while the other (javascript) uses \\ for escaping.这是因为在一种语言(语法)中,字符串中的引号用'转义,而另一种语言(javascript)使用\\进行转义。

Here an example (the actual example can contains multiple strings separated with , but , can of course also appear withing the strings):这是一个示例(实际示例可以包含多个以 分隔的字符串,但是,当然也可以与字符串一起出现):

'word'''',and''','another'',''word','''test',X,'',FUNCTION('A'' B C D')
"--------------","----------------","------",-,"",FUNCTION("---------")
"    ++++    ++","       ++ ++    ","++    ", ,"",        (" ++      ")
     1 2     3           4  5        6                       7

result after replace should be: replace后的结果应该是:

'word\'\',and\'','another\',\'word','\'test',X,'',FUNCTION('A\' B C D')

++ is a required match the opening and closing quote in the string (represented by " ) in line below the example should never be part of match (these single quotes mark the actual beginning or end of the string in which double quotes can appear). ++是必需的匹配,示例下方的字符串中的开头和结尾引号(由"表示)不应成为匹配的一部分(这些单引号标记字符串的实际开头或结尾,其中可以出现双引号) .

It seems so simple when I see it, I can point out the double quotes immediately from the string but I am unable to get it properly defined inside my pattern.当我看到它时似乎很简单,我可以立即从字符串中指出双引号,但我无法在我的模式中正确定义它。


Alternative选择

Another solution could be to replace the opening and closing quote with double quotes and replace the double occurrence of the single quite with a single single quote:另一种解决方案可能是用双引号替换开始和结束引号,并用单引号替换单引号的双重出现:

'word'''',and''','another'',''word','''test',X,'',FUNCTION('A'' B C D')
"--------------","----------------","------",-,"",FUNCTION("---------")
     ++++    ++ ,        ++ ++     , ++     , ,  ,        (  ++       )
     1 2     3           4  5        6                       7

result after replace in this case would be:在这种情况下replace后的结果将是:

"word'',and'","another','word","'test",X,"",FUNCTION("A' B C D")

Here is a prepared Fiddle to test patterns这是一个准备好的 Fiddle 来测试模式

For what it helps I came up with not working:对于它有什么帮助,我想出了不工作:

/'(('{2})+)*'/g

and

/'[^']*(('')*)*[^']*'/g

and

/('(('{2})*)*')[^,$]/g

It is not a problem: just match these strings and use a replace callback method to replace double apostrophes with single ones:这不是问题:只需匹配这些字符串并使用替换回调方法将双撇号替换为单撇号:

var expression = /'([^']*(?:''[^']*)*)'/g;
var replacer = function(m, g1){
    return g1 ? "'" + g1.replace(/''/g, '\'') + "'" : "''";
}

See this updated fiddle看到这个更新的小提琴

The '([^']*(?:''[^']*)*)' regex only matches the single quoted string literals capturing the contents in-between the single quotes. '([^']*(?:''[^']*)*)'正则表达式仅匹配捕获单引号之间内容的单引号字符串文字。

Pattern matches:模式匹配:

  • ' - leading single quote ' - 前导单引号
  • ([^']*(?:''[^']*)*) - Group 1 capturing any 0+ characters inside the '' : ([^']*(?:''[^']*)*) - 第 1 组捕获''内的任何 0+ 个字符:
    • [^']* - 0+ characters other than a single quote [^']* - 单引号以外的 0+ 个字符
    • (?:''[^']*)* - 0+ sequences of: (?:''[^']*)* - 0+ 个序列:
    • '' - two consecutive apostrophes '' - 两个连续的撇号
    • [^']* - 0+ characters other than a single quote [^']* - 单引号以外的 0+ 个字符
  • ' - closing single quote ' - 结束单引号

So, when we perform the replacement, we check of Group 1 matches and is not empty.因此,当我们执行替换时,我们检查 Group 1 匹配并且不为空。 If it is, we just return '' , and if not, we replace double apostrophes with single ones with the help of the "'" + g1.replace(/''/g, '\\'') + "'" inside the replace callback.如果是,我们只返回'' ,如果不是,我们在"'" + g1.replace(/''/g, '\\'') + "'"的帮助下用单个撇号替换双撇号替换回调。

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

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