[英]regex extract text inside quotes
In Javascript, I'm storing found regex entries to a JSON array. 在Javascript中,我将找到的正则表达式条目存储到JSON数组中。 The stored entries contain the encapsulating single quotes--but I don't want them to. 存储的条目包含封装的单引号-但我不希望它们包含在内。
The string I'm checking looks like this: 我正在检查的字符串如下所示:
{{ 'Foo' | i18n:['bar'] }}
The regex expression looks like so: regex表达式如下所示:
('[^'\\]*(?:\\.[^'\\]*)*')
This will return 'foo'
and 'bar'
when I just want it to return foo
and bar
. 当我只希望它返回foo
和bar
时,它将返回'foo'
和'bar'
。
I have the ability to just do a .replace(/'/g. '');
我有能力做一个.replace(/'/g. '');
but that doesn't help if there's an escaped single quote like so: 但这对像这样的转义单引号没有帮助:
{{ 'foo\'s' | i18n }}
Use capturing group to capture the previous charcater which must not be a backslash. 使用捕获组来捕获以前的字符,不能为反斜杠。
.replace(/(^|[^\\])'/g, '$1');
$1
refers the charcaters which are captured by the first group. $1
表示第一组捕获的字符。
Example: 例:
var s = "{{ 'foo\\\\'s' | i18n }}" alert(s.replace(/(^|[^\\\\])'/g, '$1'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.