I want to replace the "word" that is outside "span", and keep the other that is inside "span". By now, the following code works when both are following "mark>" and followed by "span". But I want to go further, following "mark>" OR being followed by "span", any one of the two condition should cause replacing action.
我想替换“span”之外的“word”,并保持“span”内的另一个。到目前为止,当以下代码都在“mark>”后跟“span”时,以下代码可以正常工作。但是我想要更进一步,在“标记>”之后或者跟随“跨度”,这两个条件中的任何一个都应该导致替换动作。
var replaceString = "newWord";
var htmlString = "This <span style='color:red' title='mark'>normal word</span> need no change. This word is to be replaced. <span>Another word</span> need no change.";
var reg=new RegExp("(?!mark>)"+replaceString+"(?!<\/span>)","gi");
var bb=htmlString.replace(reg,replaceString);
alert(bb)
// Final result should be "This <span style='color:red' title='mark'>normal word</span> need no change. This newWord is to be replaced. <span>Another word</span> need no change.";
UPDATE: using title as mark. adding starting tag span
更新:使用标题作为标记。添加起始标记跨度
UPDATE: Follow the suggestion below, I'm trying to solve the problem in anohter way, see here: js regex: replace words not in a span tag
更新:按照下面的建议,我试图以anohter的方式解决问题,请看这里:js正则表达式:替换不在span标签中的单词
2 个解决方案
#1
0
Would you be comfortable using another span
tag ? By putting a class name inside it, you should be able to change the words you need to change by changing the content of every span
containing that class.
您是否愿意使用另一个span标签?通过在其中放置类名,您应该能够通过更改包含该类的每个范围的内容来更改需要更改的单词。
Something like :
就像是 :
This <span style='color:red' mark>word</span> need no change. This <span class='changeMe'>word</span> is to be replaced. Another word</span> need no change.
此 word 无需更改。此单词 将被替换。另一个词 不需要改变。
And a jQuery script going
还有一个jQuery脚本
$('.changeMe').text("newWord")
If you still want to use Regexp, for an OR condition, you might just do it twice :
如果您仍想使用Regexp,对于OR条件,您可能只需要执行两次:
var reg=new RegExp("(?!mark>)"+replaceString,"gi");
var bb=htmlString.replace(reg,replaceString);
reg=new RegExp(replaceString+"(?!<\/span>)","gi");
bb=htmlString.replace(reg,replaceString);
#2
0
You are looking for negative look-aheads (or Lookbehinds) which JS, unfortunately, doesn't support. Check http://www.regular-expressions.info/javascript.html
您正在寻找负面预测(或Lookbehinds),但遗憾的是,JS不支持这种情况。查看http://www.regular-expressions.info/javascript.html
You may try the following Regex:
您可以尝试以下正则表达式:
var reg = new RegExp('[^(mark>)]word[^(</span>)]', "gi")
htmlString.replace(reg, " newWord "); //Check the spaces
I would rather suggest using JS to get DOM elements and replace text iterative-lly (not sure if it's a word, even a jargon).
我宁愿建议使用JS来获取DOM元素并替换迭代文本(不确定它是否是单词,甚至是行话)。
HTH
#1
0
Would you be comfortable using another span
tag ? By putting a class name inside it, you should be able to change the words you need to change by changing the content of every span
containing that class.
您是否愿意使用另一个span标签?通过在其中放置类名,您应该能够通过更改包含该类的每个范围的内容来更改需要更改的单词。
Something like :
就像是 :
This <span style='color:red' mark>word</span> need no change. This <span class='changeMe'>word</span> is to be replaced. Another word</span> need no change.
此 word 无需更改。此单词 将被替换。另一个词 不需要改变。
And a jQuery script going
还有一个jQuery脚本
$('.changeMe').text("newWord")
If you still want to use Regexp, for an OR condition, you might just do it twice :
如果您仍想使用Regexp,对于OR条件,您可能只需要执行两次:
var reg=new RegExp("(?!mark>)"+replaceString,"gi");
var bb=htmlString.replace(reg,replaceString);
reg=new RegExp(replaceString+"(?!<\/span>)","gi");
bb=htmlString.replace(reg,replaceString);
#2
0
You are looking for negative look-aheads (or Lookbehinds) which JS, unfortunately, doesn't support. Check http://www.regular-expressions.info/javascript.html
您正在寻找负面预测(或Lookbehinds),但遗憾的是,JS不支持这种情况。查看http://www.regular-expressions.info/javascript.html
You may try the following Regex:
您可以尝试以下正则表达式:
var reg = new RegExp('[^(mark>)]word[^(</span>)]', "gi")
htmlString.replace(reg, " newWord "); //Check the spaces
I would rather suggest using JS to get DOM elements and replace text iterative-lly (not sure if it's a word, even a jargon).
我宁愿建议使用JS来获取DOM元素并替换迭代文本(不确定它是否是单词,甚至是行话)。
HTH