开发者

replace words, that are not contained in links

开发者 https://www.devze.com 2023-02-16 00:24 出处:网络
I need a regex (for use开发者_如何学Go with PHP) that will do this: - replace a given word with another

I need a regex (for use开发者_如何学Go with PHP) that will do this:

- replace a given word with another

- do NOT replace this word, when it is within a href-attrib of an a-tag of HTML

example:

searchword is "foobar", replace will be "someother"

sentence1: "this is a foobar task"

sentence2: "this is a <a href="/foobar.php">foobar</a>"

will be:

1 -> "this is a someother task"

2 -> "this is a <a href="/foobar.php">someother</a>"

note that the href foobar is still there!


You may try this one:

[^href=".*/]foobar

Altough, you will have to take care about the first sign just before the foobar, which will also be replaced, i.e., >foobar -> someother. So, try some backreference, or combine it with some other regexp.

For the future, make sure that you specify the question exactly, and write what you have tried so far. Just like Bobby mentioned in his comment.

0

精彩评论

暂无评论...
验证码 换一张
取 消