user2028856
user2028856

Reputation: 3183

Javascript match and replace the following symbol

I'm trying to match and replace the following: /?& with /?

collage_url.match(/\/?&/) == '/?';

However this doesn't seem to work. Anyone able to help me out here, thanks

Upvotes: 0

Views: 40

Answers (2)

Narendra Yadala
Narendra Yadala

Reputation: 9664

You do not need regex for this. Do this collage_url = collage_url.replace('/?&', '/?')

If you want to use regex, then do this collage_url = collage_url.replace(/\/\?&/, '/?') as ? represents optional quantifier.

Upvotes: 2

falsetru
falsetru

Reputation: 369134

Use String.replace

'url/?&'.replace(/\/\?&/, '/?')
// => "url/?"

? has special meaning in regular expression: 0 or 1 match of preceding pattern; You need to escape ? to match it literally.

Upvotes: 1

Related Questions