Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use replaceAll() in Javascript.........................? [duplicate]

Tags:

I am using below code to replace , with \n\t

ss.replace(',','\n\t')

and i want to replace all the coma in string with \n so add this ss.replaceAll(',','\n\t') it din't work..........!

any idea how to get over........?

thank you.

like image 681
Warrior Avatar asked Apr 13 '11 12:04

Warrior


People also ask

How do I use replaceAll in JavaScript?

To make the method replace() replace all occurrences of the pattern you have to enable the global flag on the regular expression: Append g after at the end of regular expression literal: /search/g. Or when using a regular expression constructor, add 'g' to the second argument: new RegExp('search', 'g')

How do you replace all occurrences of a character in a string in JavaScript?

To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag.

How replace all occurrences of a string in TypeScript?

To replace all occurrences of a string in TypeScript, use the replace() method, passing it a regular expression with the g (global search) flag. For example, str. replace(/old/g, 'new') returns a new string where all occurrences of old are replaced with new .


2 Answers

You need to do a global replace. Unfortunately, you can't do this cross-browser with a string argument: you need a regex instead:

ss.replace(/,/g, '\n\t');

The g modifer makes the search global.

like image 122
lonesomeday Avatar answered Oct 11 '22 08:10

lonesomeday


You need to use regexp here. Please try following

ss.replace(/,/g,”\n\t”)

g means replace it globally.

like image 37
Eldar Djafarov Avatar answered Oct 11 '22 08:10

Eldar Djafarov