0

What is the standard way in javascript for replacing each occurrence of a certain symbol in a string with itself n times?

I have text strings where each occurrence of the single-quote symbol needs to be replaced with single-quote symbol doubled, i.e. in place of each occurrence of single-quote must appear two. This is a standard task for pre-parsing text for PostgreSQL calls.

I found out that function replace() isn't suitable for this, i.e. we cannot use it like replace('A','AA'), it doesn't work in this special case when we have a string with repeated symbols like 'Test AA', which we would want to turn into 'Test AAAA'.

So, what is the right way to do this?

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • Perhaps you're looking for a recursive replace? http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript – briosheje Mar 09 '15 at 17:22
  • *"This is a standard task for pre-parsing text for PostgreSQL calls"* - no, its a standard way only when your query is open to SQL Injection attacks. Use parameterized queries and you dont have this problem in the first place! Also executing sql queries from javascript sounds like a security nightmare waiting to happen in and of itself. – Jamiec Mar 09 '15 at 17:22
  • At previous poster, when running a NodeJS app on a server this is normal. – vitaly-t Mar 09 '15 at 18:16

1 Answers1

4

replace('A','AA') will only replace the first occurance of "A" in your string. You want a "replaceAll" which you need to use regular expression with a g flag:

'Test AA'.replace(/A/g, 'AA'); // Test AAAA

EDIT to answer your further question. To construct a ragular expression using a variable, you will need to use the RegExp constructor. Here is a verbose example:

var find = 'A';
var replacement = find + find; // AA;
var regex = new RegExp(find, 'g');
var toReplace = 'Test AA';
var replaced = toReplace.replace(regex, replacement);
console.log(replaced); // Test AAAA
rgthree
  • 7,217
  • 17
  • 21
  • And how do you do this when both text to be replaced and text to be replaced with are both variables? – vitaly-t Mar 09 '15 at 18:12
  • I'm stuck on this issue in another place now where I have to use variable text for replacement. – vitaly-t Mar 09 '15 at 18:28
  • @user1102051 You will need to use the `RegExp` constructor manually to use with a variable. I've updated the answer for you. – rgthree Mar 09 '15 at 18:42
  • Cheers! I have found out elsewhere - yes, by using RegEx object that works fine. – vitaly-t Mar 09 '15 at 18:47
  • By the way, your example doesn't quote work, if variable find contains special symbols. In my case I have to replace variables named as this: "$1", "$2", etc. so I have variable name = "$1" at some point. And to make it work I had to create new RegEx like this: new RegEx("\\" + find, replacement), that's all because I have $ as the first letter. Weird :) – vitaly-t Mar 09 '15 at 18:49
  • Yes, `$` is a special character in regular expression and you will need to escape that character (that was outside of your specific question, of course). Glad I could help. – rgthree Mar 09 '15 at 18:58