简体   繁体   English

正则表达式匹配正则表达式模式之外的所有内容

[英]Regex to match everything outside of a regex pattern

So I'd like to use javascript to replace all the words outside of HTML tags in a body of text. 因此,我想使用javascript替换正文中HTML标记之外的所有单词。 Check the explanation below. 检查以下说明。

I'd like to convert this: 我想将其转换为:

<tag with-attr="something"></tag><tag>Text to match</tag><tag>Text to Match</tag>

...to this: ...对此:

<tag with-attr="something"></tag><tag>Manipulated Text</tag><tag>Manipulated Text</tag>

Now, I have a regular expression that can match all the tags and its containing text: 现在,我有一个可以匹配所有标记及其包含文本的正则表达式:

\<[^>]*\>

But I'm not sure how to invert the expression, so to speak. 但是,我不确定如何invert表达式。

EDIT 编辑
Also, I'm looking to use the replace / match functions, not split , since I want to retain the tag information and spit the a working page back out with the new information. 另外,我希望使用replace / match功能,而不是split ,因为我想保留标签信息并向工作页面吐出新信息。

using a paren-including split() RegExp and further array methods make "stream processing" fairly simple: 使用包含paren的splitExp)RegExp和其他数组方法可以使“流处理”变得相当简单:

'<tag with-attr="something"></tag><tag>Text to match</tag>Text to Match<tag>'
  .split(/(<[^>]+>)/).map(function(x,i){
  if(!(i%2) && x){ x= escape(x); }
  return x;
}).join("");

example output: 示例输出:

"<tag with-attr="something"></tag><tag>Text%20to%20match</tag>Text%20to%20Match<tag>"

the escape() is just to show that the textContent has indeed been altered... i only vouch for input close to your example. escape()只是为了表明textContent确实已被更改...我只保证在您的示例附近提供输入。 deeply nested or invalid HTML might fool any RegExp, but i'm sure someone else will bring that up... 深度嵌套或无效的HTML可能会欺骗任何RegExp,但是我敢肯定会有其他人提出来...

Something like this 像这样

/>([^<>]*\w)</

demo here : http://rubular.com/r/2QPLjOeMAu 演示在这里: http : //rubular.com/r/2QPLjOeMAu

Now you just need to replace the content like this : 现在,您只需要替换以下内容:

var str = '<tag with-attr="something"></tag><tag>Text to match</tag><tag>Text to Match</tag>';
var res = str.replace(/>([^<>]*\w)</g, '>Manipulated text<');
console.log(res);

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

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