简体   繁体   English

仅当单词前面没有特定字符时才替换它

[英]Replace a word only if it's not preceded by a certain character(s)

I like to replace all occurrences of a string in JavaScript where the string does not start with < or / .我喜欢替换 JavaScript 中字符串不以</开头的所有字符串。 I was able to match the word but I want to replace the word only, not the preceding characters.我能够匹配这个词,但我只想替换这个词,而不是前面的字符。

var hitName1 = "body";

var testHtmlStr = "This is a test string with <body> html tag and with regular body string and another <body> html string and with no flexbody and with /body as in a url string";

var re5 = new RegExp('[^<\/]' + hitName1 , 'gi');

console.log(re5);

var testResult5 = testHtmlStr.match(re5);

console.log(testResult5);

I get the result [" body", "xbody"]我得到结果[" body", "xbody"]

If I use replace() instead of match() I will replace " body" and "xbody" with replace string.如果我使用replace()而不是match() ,我将用替换字符串替换“body”和“xbody”。 But I would like to replace only "body" with replace string.但我只想用替换字符串替换“body”。 How to do that?怎么做?

More explanation:更多解释:

use of replace:使用替换:

var testResult5 = testHtmlStr.replace(re5, "HELLO");

console.log(testResult5);

The resulting string after replacement:替换后的结果字符串:

"This is a test string with <body> html tag and with regularHELLO string and another <body> html string and with no fleHELLO and with /body as in a url string"

The replace function replaced body with HELLO , but I want to replace body (with nospace infront).替换 function 用HELLO替换了body ,但我想替换body (前面没有空格)。 Also the xbody replaced with HELLO , but I want to replace only body not xbody .也将xbody替换为HELLO ,但我只想替换body而不是xbody

Hope this is more clear.希望这更清楚。

One way you could do it is defining a capturing group around the preceding character:一种方法是围绕前面的字符定义一个捕获组:

var hitName1='body';
var testHtmlStr = "This is a test string with <body> html tag and with regular body string and another <body> html string and with no flexbody and with /body as in a url string";
var re5 = new RegExp('([^<\/]|^)' + hitName1, 'gi');

alert(testHtmlStr.replace(re5, '$1'));

jsFiddle Demo jsFiddle 演示

So if you want to replace your string with fos for example, you can write $1fos .因此,如果你想用fos替换你的字符串,你可以写$1fos

UPDATE : Following @yankee's comment I've changed the regex: added |^ to make it work when testHtmlStr starts with hitName1 or is equal to it.更新:在@yankee 的评论之后,我更改了正则表达式:添加|^以使其在testHtmlStrhitName1或等于它时工作。

well i couldn't understand what you want to do exactly but i think you are trying to replace some tags from a string that contains tags and other textNodes好吧,我不明白您到底想做什么,但我认为您正在尝试从包含标签和其他 textNodes 的字符串中替换一些标签

However, i think if you use "for" loop to verify which tags should be replace and which tags shouldn't, you will be able to do what you need.但是,我认为如果您使用“for”循环来验证应该替换哪些标签以及不应该替换哪些标签,您将能够做您需要的事情。

for(result in testResult5)
{
    if(result!="body") {// do what you want}
}

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

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