Wizard
Wizard

Reputation: 11275

Check if a string starts with a number in parentheses

My title can look like

(10) - lorem ipsum
(101) - lorem ipsum
(1) - lorem ipsum
lorem ipsum

I want to check if my title contains (number) at the start

string.match(/^(+[0-9]+)+$/);

I'm not good on regex, can someone help and say what is wrong ?

Upvotes: 4

Views: 3538

Answers (2)

Adam
Adam

Reputation: 5233

Just remove the $ from your regex, and escape the parentheses.

string.match(/^\([0-9]+\)/);

$ means end of string.

( and ) are special character and should be escaped. Pharenthesis are used for grouping. You can find a list of special characters here.

Upvotes: 8

Siva
Siva

Reputation: 2785

Use test method to return whether condition is true or false, Whether it starting with number or not.

/^\([0-9]+\)/.test(string);

OR

var patt = new RegExp(/^\([0-9]+\)/);
patt.test(string);

Upvotes: 0

Related Questions