Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript replace function using regular expressions

I am trying to use javascript's replace function to replace a string. But it just replaces the first instance. So when I use regular global expressions,

var result = 'moaning|yes you|hello test|mission control|com on'.replace(/|/g, ';');

I get: http://jsfiddle.net/m8UuD/196/

I want to get:

moaning;yes you;hello test;mission control;com on

like image 972
Samiya Akhtar Avatar asked Jun 11 '26 17:06

Samiya Akhtar


2 Answers

Simply escape the pipe :

 'moaning|yes you|hello test|mission control|com on'.replace(/\|/g, ';');

Here you'll find the list of regex special characters that you should generally escape.

like image 168
Denys Séguret Avatar answered Jun 13 '26 09:06

Denys Séguret


var result = 'moaning|yes you|hello test|mission control|com on'.replace(/\|/g, ';');
like image 35
Viele Avatar answered Jun 13 '26 08:06

Viele