Hướng dẫn replace quotes javascript
Assuming:
That should do the trick... (if your goal is to replace all double quotes). Here's how it works:
If you're trying to remove the quotes around a given string (ie in pairs), things get a bit trickier. You'll have to use lookaround assertions:
Regex explained:
The replacement is If the quotes are always going to be at the begining and end of the string, then you could use this:
or this for double and single quotes:
With input Explanation:
The replacement is done in the same way as before: we replace the match (which includes the opening and closing quotes), with everything that was inside them.
As before However, in this last case, it's far safer, faster, more maintainable and just better to do this:
Here, I'm checking if the first and last char in the string are double quotes. If they are, I'm using Tips: More on lookaround assertions can be found here
If there's a lot of quotes in the string, this might even be faster than using regex |