[英]Match text between double or single quotes
I need some regex help. 我需要一些正则表达式的帮助。 I have been banging my head on this last few hours.
最近几个小时我一直在敲打我的脑袋。 I need to match some strings in a minified file.
我需要在缩小的文件中匹配一些字符串。
Sample string: 示例字符串:
var a ='abc'; var b = 'http://a/that.dude.js/v1/'; var c = 'def'; var d = 'https://b/that.dude.js/v1/';
var basePath = "http://othersite/that.dude.js/v1/";
I want to match full text inside single or double quotes that contains that.dude.js/v1
. 我想在包含
that.dude.js/v1
单引号或双引号内匹配全文。 I tried: 我试过了:
/('|").+that.dude.js\/v1\/('|")/g
...but this matches the full line when there are multiple occurrences in the same line. ...但是当同一行中出现多次时,这与整行匹配。
My expected match will be: 我期待的比赛将是:
http://a/that.dude.js/v1/
https://b/that.dude.js/v1/
http://othersite/that.dude.js/v1/
Here is what I have tried: http://regexr.com/3cv62 这是我尝试过的: http : //regexr.com/3cv62
Try this one: 试试这个:
/(["'])[^"']+that\.dude\.js\/v1\/\1/g
The only modification was to change .
唯一的修改是改变
.
to [^"']
this doesn't allow quotes between the quotes. to
[^"']
这不允许引号之间的引号。
If you have single quotes inside double quoted strings, you need to capture the quote delimiter and use a backreference to match exactly the same trailing delimiter: 如果双引号字符串中有单引号,则需要捕获引号分隔符并使用反向引用来匹配完全相同的尾部分隔符:
(['"])([^"'\s]*that\.dude\.js\/v1[^"'\s]*)\1
See the regex demo . 请参阅正则表达式演示 。
Since you have URLs, you can safely match them with [^"'\\s]*
(one or more symbols other than "
, '
and whitespace). 由于您有URL,因此可以安全地将它们与
[^"'\\s]*
(除"
, '
和空格之外的一个或多个符号)匹配。 The regex matches: 正则表达式匹配:
(['"])
- the leading quote delimiter (captured into Group 1 so that we could match the same trailing delimiter) (['"])
- 前导引号分隔符(捕获到组1中,以便我们可以匹配相同的尾随分隔符) ([^"'\\s]*that\\.dude\\.js\\/v1[^"'\\s]*)
- Group 2 matching ([^"'\\s]*that\\.dude\\.js\\/v1[^"'\\s]*)
- 第2组匹配
[^"'\\s]*
- 0+ symbols other than "
, '
and whitespace [^"'\\s]*
- 除"
, '
和空白以外的0+符号 that\\.dude\\.js\\/v1
- that.dude.js/v1
that\\.dude\\.js\\/v1
- that.dude.js/v1
[^"'\\s]*
- ibid. [^"'\\s]*
- 同上。 \\1
- trailing delimiter that is the same as the leading one \\1
- 尾随分隔符与前导分隔符相同 The result will be in Group 2: 结果将在第2组:
var re = /(['"])([^"'\\s]*that\\.dude\\.js\\/v1[^"'\\s]*)\\1/g; var str = 'var a =\\'abc\\'; var b = \\'http://a/that.dude.js/v1/\\'; var c = \\'def\\'; var d = \\'https://b/that.dude.js/v1/\\';\\nvar basePath = "http://othersite/that.dude.js/v1/";'; var res = []; while ((m = re.exec(str)) !== null) { res.push(m[2]); } document.body.innerHTML = "<pre>" + JSON.stringify(res, 0, 4) + "</pre>";
Note that to make it even more generic, you could use a tempered greedy token : 请注意,为了使其更通用,您可以使用淬火贪婪令牌 :
(['"])((?:(?!\1).)*that\.dude\.js\/v1(?:(?!\1).)*)\1
^^^^^^^^^^^^ ^^^^^^^^^^^^
See another demo 看另一个演示
The (?:(?!\\1).)
token will match any character(s) but a newline that are not equal to the value referred to by the \\1
backreference. (?:(?!\\1).)
令牌将匹配任何字符,但换行符不等于\\1
反向引用所引用的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.