How can I remove a specific character, a double-quote ( "
), appearing any number of times, from the start and end of a string?
I had a look at string.trim()
, which trims any whitespace characters, but it's not possible to provide an optional argument with "
as the needle to search for.
You can use RegEx to easily conquer this problem:
myString = myString.replace(/^"+|"+$/g, '');
You can substitute the "
with any character (be careful, some characters need to be escaped ).
Here's a demo on JSFiddle .
An explanation of the regular expression:
/
- start RegEx ( /
)
^"+
- match the start of the line ( ^
) followed by a quote ( "
) 1 or more times ( +
)
|
- or
"+$
- match a quote ( "
) 1 or more times ( +
) followed by the end of the line ( $
)
/
- end RegEx ( /
)
g
- "global" match, ie replace all
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.