48

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?

Adam V
  • 6,256
  • 3
  • 40
  • 52

9 Answers9

80

Try this:

yourstring = yourstring.replace(/'/g, "''")
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • @YumYumYum, in that case you don't need "replace a single-quote with two single-quotes", but to encode that additional apostrophes. – Rubens Farias May 21 '15 at 08:22
  • How do you do that what you just mentioned please? I have tried my way but failed, i need to have single quote and double quotes as it is for display and submit. –  May 22 '15 at 06:30
  • @YumYumYum, ask a new question! – Rubens Farias May 22 '15 at 11:07
6

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:

rogal111
  • 5,874
  • 2
  • 27
  • 33
5

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

Be sure to use the global match flag (g) so that you replace any and all occurrences in the string. More info here.

Evan Meagher
  • 4,517
  • 21
  • 18
  • That makes a double quote from a single quote, but does not replace a single quote by two single quotes, as the OP asks. – Jesper Oct 22 '09 at 21:10
4

JS's string.replace uses RegEx

Not necessarily:

var str = "O'Reilly's books";
alert(str.replace("'", "''", 'g'));

MDC's String replace reference:

The pattern can be a string or a RegExp

Mmm, my code above doesn't seem to work on IE6, so that will be:

str.replace(/'/g, "''")

like the others said, but using regexes for such simple operation is overkill.

PhiLho
  • 40,535
  • 6
  • 96
  • 134
3

Try this:

function QuoteEncoding(strvalue) {
    var strquotes = /(')/g;
    return "'" + strvalue.replace(strquotes, "''") + "'";
}

call this method as follows:

QuoteEncoding(strvalue);
bensiu
  • 24,660
  • 56
  • 77
  • 117
surender
  • 31
  • 1
2

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.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • I was using IE7 and was wondering why all of the answers above this weren't working. Once I implemented this str.split().join() all of my apostrophes were replaced correctly. For whatever reason the "g" was being ignored. Thanks bobince to sharing fundamentals! – fenix Jul 22 '13 at 14:11
1

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.

Franz
  • 11,353
  • 8
  • 48
  • 70
  • this does not work. But, I really want it too because I like the idea behind the idempotentness of this tranlation – Nathan Feger Oct 28 '11 at 14:52
  • Here is the answer: http://stackoverflow.com/questions/6070275/regular-expression-match-only-non-repeated-occurrence-of-a-character – Nathan Feger Oct 28 '11 at 14:56
1

You can use String#replaceAll.

const str = "'test'";
const replaced = str.replaceAll("'", "''");
console.log(replaced);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • Simple, yet powerful! Based on my limited knowledge of JavaScript, it looks like `replace` and `replaceAll` functions in JavaScript behave exactly in the opposite manner as they do in Java. – Arvind Kumar Avinash May 30 '21 at 12:44
0
js> s = "abc'def'xyz"
abc'def'xyz
js> s.replace(/'/g, "''")
abc''def''xyz
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329