Match 2 character next to each other regex

950 views javascript
2

I'm newbie in regex. I want create a regex for:

  • Match '--' in first
  • Match '$' in last
  • Match '=$' next to each other.

Here is something I try to match '--' in first and '$' in last but not work

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

String I want to pass this regex: --hostname=$HOSTADDRESS$

How can I do that. Please help me

answered question
David

Do you mean like this: /--(.*)=\$(.*)\$/? You can leave off the brackets if you don't need to capture the groups.

nick

@david you should have anchors on your regex

Akashii

@david thanks you

David

@Nick there's not enough info in the question to decide if OP wants it anchored at the ends like the example given, or anchored by spaces like I'd expect, or if lazy qualifiers would be sufficient. The meat of the regex is what they're struggling with and my comment was to clarify that point, not to provide an answer.

nick

@david Just going on the basis that OP had them on their regex...

Add a Comment

1 Answer

9
Dav?d 0 Comments

/^--.*=\$([^\$]+)\$$/gm

You need to include the m flag after /g to enable multiline ^ and $.

I don't know what you were trying to do with [--], but to match =$ you need to include =\$ in your regex.

posted this

Please login first before posting an answer.