MBaas
MBaas

Reputation: 7530

JS-RegEx not working

I have a string that has one or more {numeric}_{numeric} combinations (separated with comma) and I'd like to remove one specific combo.

('6_4,6_5,6_6').replace('\d+_5(,|$)','');

but it's not working as expected and I just do not see why. (tested in Firefox JS-Console)

Upvotes: 0

Views: 48

Answers (4)

NeutronCode
NeutronCode

Reputation: 365

You need to use regex syntax with / and not a string

('6_4,6_5,6_6').replace(/\d+_5(,|$)/,'');

Upvotes: 2

Adam
Adam

Reputation: 6733

Because you are passing the regexp as a string, not as a regexp. Try:

('6_4,6_5,6_6').replace(/\d+_5(,|$)/,'')

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

You are telling it to replace a string.

.replace(/\d+_5/,'');

That should do it.

Upvotes: 2

Sam
Sam

Reputation: 20486

Use the /.../ delimiters in stead of '...', otherwise Javascript will try to match that String (not your expression).

'6_4,6_5,6_6'.replace(/\d+_5(,|$)/,'');

Also, the () around your initial String were unnecessary (although did not cause any problems).

Upvotes: 4

Related Questions