Regex for replacing a single-quote with two single-quotes

前端 未结 8 1459
迷失自我
迷失自我 2020-12-16 09:24

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 ne

8条回答
  •  萌比男神i
    2020-12-16 09:44

    Note that if you don't want to use RegExp (and there are often good reasons not to), the idiom for a simple string replacement is:

    str.split("'").join("''")
    

    Although the RegExp version is typically marginally faster, the string version can be a win when you don't know if there might be regex-special characters (like .) in the search string.

提交回复
热议问题