PDA

View Full Version : String Replace



vinothsencha
10 Aug 2013, 3:56 AM
We need to replace a string with another string in all occurence of actual string without checking case sensitive.


The following is an example of sample code:
var formula = "Start Reading+Start Reading+End Reading";
var column = "Start Reading";
var value = "10";


formula = formula.replace(column, value, "gi");


This is giving result as "10+Start Reading+End Reading".
But we need as "10+10+End Reading" (Need to replace in all places)


Can you please tell me what needs to be changed in replace function?

skirtle
10 Aug 2013, 5:15 AM
'Start Reading+Start Reading+End Reading'.replace(/Start Reading/gi, '10');

or


'Start Reading+Start Reading+End Reading'.replace(new RegExp('Start Reading', 'gi'), '10');

However, I'm not sure what this question has to do with ExtJS.

vinothsencha
10 Aug 2013, 6:12 AM
Yes. This is not related to ExtJS.


The first part of code is working when we supply string directly:
'Start Reading+Start Reading+End Reading'.replace(/Start Reading/gi, '10');


But it is not working when we supply the string variable.
formula.replace(/column /gi, value);


I need to know how to pass the regular expression?


The second part of code is working in both value and variable.


Thanks for the code provided.