Skip to content Skip to sidebar Skip to footer

Text Between Two Dollar Signs Javascript Regex

I'm trying to use RegEx to select all strings between two dollar signs. text = text.replace(/\$.*\$/g, 'meow'); I'm trying to turn all text between two dollar signs into 'meow' (p

Solution 1:

That's pretty close to what you want, but it will fail if you have multiple pairs of $text$ in your string. If you make your .* repeater lazy, it will fix that. E.g.,

text = text.replace(/\$.*?\$/g, "meow");

Solution 2:

I see one problem: if you have more than one "template" like

aasdasdsadsdsa $a$ dasdasdsd $b$ asdasdasdsa

your regular expression will consider '$a$ dasdasdsd $b$' as a text between two dolar signals. you can use a less specific regular expression like

/\$[^$]*\$/g

to consider two strings in this example

Post a Comment for "Text Between Two Dollar Signs Javascript Regex"