Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to capture both '?' and '%3f' in a javascript replace() method

I'm terrible with Regex, can't find a suitable answer on Stack which works for this.

I have a string like this:

 var str = 'abc?def%3f999%3F^%&$*'

I only want to remove the following:

?, %3f and %3F (the entity codes for question marks)

I've tried this:

 var theQuery = str.replace([\\?]|\\%3f|\\%3F,'');

But this doesn't appear to be valid regex. What's a solution that will work here?

like image 648
JVG Avatar asked Jun 09 '26 02:06

JVG


1 Answers

You can use this:

var str = 'abc?def%3f999%3F^%&$*'
var theQuery = str.replace(/\?|%3f/gi, '');
//=> abcdef999^%&$*
  • You need to use regex delimiters / and /
  • You need to use global switch g
  • No need to double escape
  • No need to escape %
like image 142
anubhava Avatar answered Jun 11 '26 14:06

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!