[英]Regex & JS how to regex match a string between " and partial patterns (match first few characters and no special pattern)
I been been struggled to make this regex and would love to get some help here.我一直在努力制作这个正则表达式,很想在这里得到一些帮助。
So I want to match an url string if it is所以我想匹配一个 url 字符串,如果它是
.dont_match1
or .dont_match1/
at the end最后不包含.dont_match1
或.dont_match1/
类的模式then replace example.com
to example2.com
.然后将example.com
替换为example2.com
。
for example,例如,
bla ...... "https://example.com/content/a.dont_match1"
bla ...... "https://example.com/content/a.dont_match2"
No match不匹配
href="https://example.com/"
Matched and replace to =>href="https://example2.com/"匹配并替换为 =>href="https://example2.com/"
<link rel="canonical" href="https://example.com adasd /" />
No match because of the stupid space由于愚蠢的空间没有匹配
<link rel="manifest" href="https://example.com/a/asd/aaaa">
Matched and replace to =><link rel="manifest" href="https://example2.com/a/asd/aaaa">匹配并替换为 =><link rel="manifest" href="https://example2.com/a/asd/aaaa">
All these lines are in a file.所有这些行都在一个文件中。
Been stuck on these for a while, have tried quite a few, but not working well坚持了一段时间,尝试了很多,但效果不佳
(=".*)(example.com)([^\\s])*"
(=".*)(example.com)([^\\s|^.dont_match1 |^.dont_match2])*"
You can use您可以使用
/("https:\/\/)example\.com(?![^\s"]*\.(?:dont_match1|dont_match2)\/?")([^\s"]*")/g
Repace with $1example2.com$2
.替换$1example2.com$2
。 See the regex demo .请参阅正则表达式演示。
Details细节
("https:\\/\\/)
- Group 1 ( $1
): "https://
string ("https:\\/\\/)
- 第 1 组 ( $1
): "https://
字符串example\\.com
- an example.com
string example\\.com
- 一个example.com
字符串(?![^\\s"]*\\.(?:dont_match1|dont_match2)\\/?")
- a negtive lookahead that fails the match if there are zero or more chars other than whitespace and "
followed with a .
, then either dont_match1
or dont_match2
, then an optional /
and then a "
immediately to the right of the current location (?![^\\s"]*\\.(?:dont_match1|dont_match2)\\/?")
- 如果有零个或多个字符而不是空格和"
后跟一个.
,则匹配失败的负前瞻要么dont_match1
或dont_match2
,然后可选/
再一个"
立即到当前位置的右([^\\s"]*")
- Group 2 ( $2
): zero or more chars other than whitespace and "
and then a "
char. ([^\\s"]*")
- 第 2 组 ( $2
):除空格和"
之外的零个或多个字符,然后是"
字符。JavaScript demo: JavaScript 演示:
const array = ['bla ...... "https://example.com/content/a.dont_match1"', 'bla ...... "https://example.com/content/a.dont_match2"', 'href="https://example.com/" ']; const rx = /("https:\\/\\/)example\\.com(?![^\\s"]*\\.(?:dont_match1|dont_match2)\\/?")([^\\s"]*")/g; array.forEach( x => console.log(x, '=>', x.replace(rx, '$1example2.com$2')) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.