简体   繁体   English

正则表达式匹配尖括号外的文本

[英]Regular expression to match text outside angle brackets

I'm trying to replace all specific word occurrences outside angle brackets.我正在尝试替换尖括号外的所有特定单词。

For example:例如:

I want to replace "is" word in this string:我想替换这个字符串中的“是”字:

this is a <sample> string with <some> special words. this <is another> one

Find all "is" words out side the angle brackets, expected output:找到尖括号外的所有“是”字,预期输出:

th*is* *is* a <sample> string with <some> special words. this <is another> one

You can use a regular expression with a negative look-ahead, which will check that there is no closing > following a potential match, without any preceding < :您可以使用带有否定前瞻的正则表达式,这将检查潜在匹配之后是否没有结束> ,而没有任何前面的<

 let s = "this is a <sample> string with <some> special words. this <is another> one"; let result = s.replace(/is(?![^<>]*>)/g, "*$&*"); console.log(result);

Note that it will also match a third "is" by that logic.请注意,它还将通过该逻辑匹配第三个“是”。

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

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