- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2015 11:57 PM
Hello all ,
I am trying to replace all the contents present in one variable in another variable dyamically as below.
var src = g_form.getValue('U_test');
var src1= g_form.getValue('U_test1');
var src2 = g_form.getValue('U_test2');
var my_new_str = src.replace(/A/g,'M') ;********************This is working fine as it is replacing all 'A' with 'M'.
But i need to replace the content of src1 present in src dynamically with src2 like,
var my_new_str = src.replace(src1 /g,src2 );*******************This is not working.
Tried with other Java functions to replace strings .But it didnt work.
Does anyone know if there is any correct way to do this in Service Now?
Thanks ,
Adarsh
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2015 12:37 AM
Hi Adarsh,
Use the below code which works for your requirement. You need a regular expression in this case.
var src = g_form.getValue('u_test');
var src1= g_form.getValue('u_test1');
var src2 = g_form.getValue('u_test2');
var exp = new RegExp(src1, 'gi'); //'gi' is for case insensitive replacement and 'g' for case sensitive replacement
var my_new_str = src.replace(exp,src2 );
Regards,
Mamatha Thaluri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2015 12:37 AM
Hi Adarsh,
Use the below code which works for your requirement. You need a regular expression in this case.
var src = g_form.getValue('u_test');
var src1= g_form.getValue('u_test1');
var src2 = g_form.getValue('u_test2');
var exp = new RegExp(src1, 'gi'); //'gi' is for case insensitive replacement and 'g' for case sensitive replacement
var my_new_str = src.replace(exp,src2 );
Regards,
Mamatha Thaluri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2015 02:59 AM
Thank you Mamatha .