0

i am trying to write a regular expression for matching any words like which come in between "$" for example var myString = " there was a $dog$# and a $rat$ in a town"; i want to match dog and rat i have written the expresion for it

       var reg = /^$(.*)$/gmi 

but that does not because , the "$" being special character of end of line , how can i escape it , can i use "\" ?? can you pelase suggest the correct expression .

2

5 Answers 5

2

There is a problem with greediness, as you mentioned in a few comments. * will consume as much as possible, so if it is used like .* it will also consume further $ if it can find another one later one. There are two possibilities. The quickest and most common one is to simply make that quantifier ungreedy:

/\$(.*?)\$/g

Now it will consume as little as possible. The recommended way however is to make the repetition and the delimiter mutually exclusive and keep the repetition greedy:

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

This is generally more efficient, because no backtracking is needed. (Please don't hold me up on that, in any specific case the backtracking version might still be more efficient, but this is generally recommended practice)

Sign up to request clarification or add additional context in comments.

Comments

1

As you said you should use '\' to escape the special characters. Also '^' is the start of the string you want to match for. You should use the following:

/\$(.*)\$/g

5 Comments

@matov , if i use /\$(.*)\$/gmi in the string "there is a '$game$' which talks about the game and a $dog$$" it will return the whole thing from game......dog
What are m and i for in this case?
@m.buettner: m makes sense if you have "...$do\ng$..." (which doesn't make too much sense, but I don't know OP's data). i is useless.
You mean "s" instead of "m" ;). "m" makes ^ and $ match the start and end of lines.
yeah, right, soz... just copied and pasted the gmi bit. Early hours in Europe:)
1

Yes, escaping is done with \ -s, if you want to catch individual $...$ marked words try this regexp:

/\$([^$]+)\$/gmi // inside character classes ( the []-s) you don't need to escape it, but if you do it does the same thing

Comments

1
/\$(.*)\$/

You use \ for escaping, as you thought. Also, given your requirements, ^ (start of string) is detrimental.

In case you have multiple appearances of such strings, you have to be careful of greediness (the default), so you either disallow the terminator from the contents:

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

or you make the repetition operator non-greedy:

/\$(.*?)\$/g

4 Comments

can you look at the comment i made to matov's post
Yup. Clarified; I didn't notice your modifiers.
@m.buettner: lol, not a competition... (despite the page keeping the score :p )
Sure, my comment wasn't meant to be taken too serious ;)
0

can i use "\" ??

Yes.

Why didn't you try that yourself?

var reg = /^\$(.*)\$/gmi

will match:

  • Start of string
  • A $
  • Zero of more characters
  • A $

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.