I have the string test1 test2 tes<span style="color:red;">t3</span> span test4 and I want to put span tag surrounding the span word which position is after </span> without touching the existing span tag using javascript regex.
1 Answer
REgex:
(<\/span> )(span)
REplacement string:
$1<span>$2</span>
> 'test1 test2 tes<span style="color:red;">t3</span> span test4'.replace(/(<\/span> )(span)/g, "$1<span>$2</span>")
'test1 test2 tes<span style="color:red;">t3</span> <span>span</span> test4'
2 Comments
DLP
For the current example your answer is correct, but lets think more golabaly. I want to do the same think but with any word or part of word that is close to the text from the span tag. For example
pa from spanDLP
That is close but the result I want is this:
'test1 test2 tes<span style="color:red;">t3</span> s<span>pa</span>n test4' is it possible?
str.replace('span', '<span>span</span>')?test1 test2 tes<<span>span</span> style ...