0

I'm trying to do a specify regex

In php i can do:

'/[lg|sm|md|xs]/'

Will match or lg, or sm, or md or xs once.

On javascript i can't make it work property.

var href = $(this).attr('href');
var t = href.replace('[lg]|[sm]|[md]|[sx]', 'add'); //not working

var t = href.replace('[lg|sm|md|sx]', 'add'); //not working

var t = href.replace('/[lg|sm|md|sx]/', 'add'); //not working

var t = href.replace('/lg|sm|md|sx/', 'add'); //not working

For a URL like:

href="/img/galeria/lg/duplo-standart/foto1.jpg"

All I need is to replace for "lg" on the href and change to "add".

Help is appreciated.

0

1 Answer 1

3

You need to use a regex notation or object so

var t = href.replace(/(lg|sm|md|sx)/, 'add');

or

var t = href.replace(new RegExp('(lg|sm|md|sx)'), 'add');

what you are doing is a string replacement, so it will search of a exact string match not a regex match

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

1 Comment

Fast and Furious! Thank You.

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.