2

I'm trying to replace all characters that are any of the following: !# in a javascript string (specifically a URL).

href = 'http://example.test/foo/bar#';
href = href.replace('/[\#\!]/g', '');
console.log('href', href);

But it doesn't seem to be performing the replacements, even though I tried the regex with various testers and it works with the string.

2
  • 10
    regex isn't a string, '/[\#\!]/g' => /[\#\!]/g Commented Jan 29, 2018 at 2:47
  • 1
    Also # and ! are not special characters in Javascript regex, you don't have to escape them. Commented Jan 29, 2018 at 3:22

1 Answer 1

3

You only have to delete the quotes from '/[#!]/g' and it is going to work.

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

Comments

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.