0

I'm trying to detect string variants: {ext_1} or {ext_1alpha} or {ext_aplpha1} My regular expression: /{ext_(^[0-9,a-z]+$)}/gi

var arr = str.match(/{ext_(^[0-9,a-z]+$)}/gi);

But this is wrong. How can I solve this problem?

3 Answers 3

2

You have anchors (^ and $) in the middle of the regex where they can't ever match.

Use

/\{ext_([A-Z0-9]+)\}/gi

You also don't want a comma in your character class, unless you want to match actual comma characters in your string. Also, it's a good idea to escape curly braces because they can have special meaning in a regex.

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

4 Comments

Notably, the curly brackets also needed escaping, as shown in your changed regex. :)
@UweB: Not really; they only need to be escaped where they could be misinterpreted (which is not the case here). But it's still good practice to do so.
Doesn't work. This expression can detect only numbers: /{ext_(\d+)}/gi b and it works but my string can contains numbers or alphanumbers...
@virtualnobi: Oops. Where did that come from? Thanks for noticing this.
1

This works-

/{ext_([a-z0-9]+)}/gi

Comments

1

Try this

/{ext_([[:alnum:]]+)}/

see here

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.