简体   繁体   English

我们如何通过在 javascript 中使用正则表达式转义撇号来选择两个单引号之间的字符串?

[英]How can we select string between two single quotes by escaping apostrophe using regex in javascript?

Example Text: 'builder's margin' means the percentage stated in Item 8 of Schedule 1;示例文本: 'builder's margin' means the percentage stated in Item 8 of Schedule 1;

Here I have a regex that selects words between two single quotes /'(.*?[^\\\\])'/g .这里我有一个正则表达式,它在两个单引号之间选择单词/'(.*?[^\\\\])'/g But it's not working when i try to extract builder's margin from the example text because there is apostrophe so it's only selecting up to builder .但是当我尝试从示例文本中提取builder's margin时它不起作用,因为有撇号所以它只选择builder so is there any way that we can escape apostrophe and select up to margin??那么有什么方法可以让我们摆脱撇号并选择边距?

Assuming the middle apostrophe has no word character right after it, you can try this regex:假设中间的撇号后面没有单词字符,你可以试试这个正则表达式:

'(?:\\.|'(?=\w)|[^'])*'

RegEx Demo正则表达式演示

RegEx Details:正则表达式详情:

  • ' : Start opening ' ' : 开始打开'
  • (?: : Start non-capture group (?: : 启动非捕获组
    • \\\\. : Match \\ and an escaped character : 匹配\\和一个转义字符
    • | : OR : 或者
    • '(?=\\w) : Match ' if it has a word character next to it '(?=\\w) : 匹配'如果它旁边有一个单词字符
    • | : OR : 或者
    • [^'] : Match any character that is not ' [^'] : 匹配任何不是'字符
  • )* : End non-capture group. )* : 结束非捕获组。 Repeat this group 0 or more times重复此组 0 次或更多次
  • ' : Closing ' ' : 关闭'

If you do not need to support escape sequences (ie if your string is not a string literal presented as plain text), you can use如果您不需要支持转义序列(即,如果您的字符串不是以纯文本形式呈现的字符串文字),则可以使用

(?!\b'\b)'([^']*(?:\b'\b[^']*)*)(?!\b'\b)'

Else, if the text you have can contain escape sequences, you can use否则,如果您拥有的文本可以包含转义序列,则可以使用

(?!\b'\b)'([^'\\]*(?:(?:\\.|\b'\b)[^'\\]*)*)(?!\b'\b)'

See the regex demo #1 and regex demo #2 .请参阅正则表达式演示 #1正则表达式演示 #2

NOTE : replace .注意:更换. with [^] to match any chars including line break chars.[^]匹配任何字符,包括换行符。

Details细节

  • (?!\\b'\\b)' - the ' char that is not enclosed with word chars on both ends (?!\\b'\\b)' - '两端没有用字符字符括起来的字符
  • ([^']*(?:\\b'\\b[^']*)*) - Capturing group 1: ([^']*(?:\\b'\\b[^']*)*) - 捕获组 1:
    • [^']* - zero or more chars other than ' [^']* - 除'之外'零个或多个字符
    • (?:\\b'\\b[^']*)* - zero or more occurrences of a ' not enclosed with word chars and then zero or more chars other than ' (?:\\b'\\b[^']*)* - 零次或多次出现的'没有用单词字符括起来,然后是零个或多个除'以外'字符
  • ([^'\\\\]*(?:(?:\\\\.|\\b'\\b)[^'\\\\]*)*) - Group 1 (in the second pattern): ([^'\\\\]*(?:(?:\\\\.|\\b'\\b)[^'\\\\]*)*) - 第 1 组(在第二种模式中):
    • [^'\\\\]* - zero or more chars other than ' and \\ [^'\\\\]* - 除'\\之外'零个或多个字符
    • (?:(?:\\\\.|\\b'\\b)[^'\\\\]*)* - zero or more sequences of a \\ + any one char or a ' that is enclosed with word chars and then zero or more chars other than ' and \\ (?:(?:\\\\.|\\b'\\b)[^'\\\\]*)* - 零个或多个\\ + 任何一个字符或用单词字符括起来的'序列,然后是零或'\\以外'更多字符
  • (?!\\b'\\b)' - the ' char that is not enclosed with word chars on both ends (?!\\b'\\b)' - '两端未用字符字符括起来的字符

Try尝试

'(.+?)'\B

which basically means ' , then some content, then ' , followed by a non-word char.这基本上意味着' ,然后是一些内容,然后是' ,然后是一个非单词字符。

 a = `normal 'quoted' string` b = `'builder's margin' means the percentage` c = `this 'we'll handle'` re = /'(.+?)'\\B/ console.log(a.match(re)[1]) console.log(b.match(re)[1]) console.log(c.match(re)[1])

Note that this doesn't handle trailing apostrophes as in 'builders' margins' are...请注意,这不处理尾随撇号,因为在'builders' margins' are...

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

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