I'm running into an issue that I think is being caused by needing to double-up on some single quotes inside a string. However, JS's string.replace uses RegEx, and I've never built a RegEx by hand.
Can someone help me build a RegEx to find a single quote and replace it with two single quotes?
Use the String. replace() method to replace double with single quotes, e.g. const replaced = str. replace(/"/g, "'"); . The replace method will return a new string where all occurrences of double quotes are replaced with single quotes.
To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.
In HTML, CSS and JavaScript code, single and double quotes are interchangeable.
Use the String. replace() method to replace single with double quotes, e.g. const replaced = str. replace(/'/g, " ); . The replace method will return a new string where all occurrences of single quotes are replaced with double quotes.
Try this:
yourstring = yourstring.replace(/'/g, "''")
You don't need to use RegExp.
String patterm version:
str.replace("'", "''", 'g')
RegExp pattern version:
str.replace(/'/g, "''")
Here you have some useful RegExp links:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With