Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
182
rated 0 times [  188] [ 6]  / answers: 1 / hits: 22192  / 13 Years ago, fri, december 2, 2011, 12:00:00

I have to escape two special characters and , in the string with the following rules.



Example:-




  • Mercury should be converted into Mercury

  • Mercu,ry should be converted into Mercu,ry

  • Mercu,ry should be converted into Mercu,ry



Rules:-




  • Meaning comma or double quote should be escaped with double quote.

  • Comma will escaped by wrapping the whole word in double quotes.

  • If Double quote is found, then it double quote should be added at its
    position. Also the whole word should be wrapped inside the double
    quotes.



Please suggest the regex pattern in javascript.


More From » regex

 Answers
17
var test = [
'Mercury', 'Mercu,ry', 'Mercu,ry', 'Mercury'
];


for (x in test) {
var s = test[x];
if (s.indexOf('') != -1) {
s = s.replace(//g, '');
}

if (s.match(/|,/)) {
s = '' + s + '';
}

alert(s);
}


Test: http://jsfiddle.net/ZGFV5/



Try to run the code with Mercury :)


[#88791] Wednesday, November 30, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wyattkennyc

Total Points: 650
Total Questions: 102
Total Answers: 90

Location: Monaco
Member since Mon, May 23, 2022
2 Years ago
;