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

前端 未结 8 1451
迷失自我
迷失自我 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条回答
  • 2020-12-16 09:57

    I don't know the exact syntax, but you can find that out yourself:

    str.replace(/(?!')'(?!')/g, "''");
    

    Haven't tested this yet, but if it works, it also takes care of only replacing occurances of one single quote in a row. The g modifier is necessary for replacing all ocurrences.

    0 讨论(0)
  • 2020-12-16 09:58

    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:

    • RegExp Tutorial
    • Regular Expression Basic Syntax Reference
    • RegExp Build&Testing Online Tool
    0 讨论(0)
提交回复
热议问题