note: I'm not trying to parse HTML with regular expression
I am trying to replace any content enclosed in $ sign ($, for example $) in a string. I managed to find str.replace(/\$([^\$]*)\$/g), "hello $1!"), but I had problems with the fact that I do not replace such strings when they are wrapped in HTML tags.
str.replace(/\$([^\$]*)\$/g), "hello $1!")
Example line: $someone$, <a>$welcome$</a>, and $another$
$someone$, <a>$welcome$</a>, and $another$
Expression: /[^>]\$([^\$]*)\$[^<]/g
/[^>]\$([^\$]*)\$[^<]/g
Expected Result: hello someone!, <a>$welcome</a>, and hello another!
hello someone!, <a>$welcome</a>, and hello another!
Actual output: $someonhello , !elcomhello , and !nother$
$someonhello , !elcomhello , and !nother$
Test code: alert("$someone$, <a>$welcome$</a>, and $another$".replace(/[^>]\$([^\$]*)\$[^<]/g, "hello $1!"));
alert("$someone$, <a>$welcome$</a>, and $another$".replace(/[^>]\$([^\$]*)\$[^<]/g, "hello $1!"));
fiddle: http://jsfiddle.net/WMWHZ/
Thank!
, 6 '$' . , , , " > ", , 4 5 , "</a>, and " .
"</a>, and "
:
$('div').text(test.replace(/(^|[^>])\$([^<][^\$]*)\$(?!<)/g, "$1hello $2!"))
Javascript lookbehinds , lookaheads ( (?!<)). lookbehinds, [^>] , , .
(?!<)
[^>]
, "$" , .
, , , , '<' , [^<] . , ( "$$" ), .
[^<]
, .