JavaScript.Replace content enclosed in dollar signs ($) outside HTML tags

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.

Example line: $someone$, <a>$welcome$</a>, and $another$

Expression: /[^>]\$([^\$]*)\$[^<]/g

Expected Result: hello someone!, <a>$welcome</a>, and hello another!

Actual output: $someonhello , !elcomhello , and !nother$

Test code: alert("$someone$, <a>$welcome$</a>, and $another$".replace(/[^>]\$([^\$]*)\$[^<]/g, "hello $1!"));

fiddle: http://jsfiddle.net/WMWHZ/

Thank!

+3
source share
1 answer

, 6 '$' . , , , " > ", , 4 5 , "</a>, and " .

:

$('div').text(test.replace(/(^|[^>])\$([^<][^\$]*)\$(?!<)/g, "$1hello $2!"))​

Javascript lookbehinds , lookaheads ( (?!<)). lookbehinds, [^>] , , .

, "$" , .

, , , , '<' , [^<] . , ( "$$" ), .

, .

+4

All Articles