I am using following code, in which i want to replace all ' with ;$39 but its not working fine . It's replace only first ' .
var searchUserName = document.getElementById("ctl00_ContentMain_UserSearchColl").value.replace("/\'/g", ";$39;");
For example: Ram's's .Output: Ram;$39s;$39s
Thanks in advance.
You don't need to put the regexp inside double quotes. Remove them.
value.replace(/'/g, ';$39;')
Also note that you need not "escape" the single quote. (Thanks #Paul S. for pointing)
Related
I have a string which looks like this
var dragdropMatchResponseData = '2838[,]02841[:]2839[,]02838[:]2840[,]02840[:]2841[,]02839';
I want to replace the following
1: '[,]' into ':'
2: '[.]' into ','
I tried the following
console.log(dragdropMatchResponseData.replace({ '[,]': ':', '[:]': ',' }));
and
console.log(dragdropMatchResponseData.replace('[,]', ':').replace( '[:]', ','));
but nothing helped me
I want my end result to look like
'2838:02841,2839:02838,2840:02840,2841:02839';
I don't want to add replace in multiple times, I want to do this at one time,
how can I achieve this?
Try regular expression
dragdropMatchResponseData.replace(/\[,\]/g, ':').replace(/\[:\]/g, ',')
The /g flag is to replace all the occurances within the string.
Hey It can be easily achieved using replace function of JS
var data = '2838[,]02841[:]2839[,]02838[:]2840[,]02840[:]2841[,]02839';
console.log(data.replace(/\[:]/g, ',').replace(/\[,]/g, ':'))
In my web application I get data from Instagram. Some full names have single quotes like:
♠ فال ورق ' تاروت ' پیشگویی
I'm trying to remove or replace them with an empty character, this code doesn't work and I can't replace them:
.replace(/("|')/g, "")
or
.replace(/["']/g, "")
How can I change this code to remove the single quotes?
How about this solution? Hope it helps!
console.log("فال ورق ' تاروت ' پیشگویی".replace(/'/g, ""));
console.log("code'123".replace(/'/g, ""));
I have problem in removing ' with (blank and no space). Like Kello's to kellos.
I already tried this-
str = str.replace(/[\']/g, '');
But its not working.
Please help.
It actually does work:
var str = 'aa\'bb\'cc';
alert(str.replace(/'/g,'')); // aabbcc
alert(str.replace(/[\']/g,'')); // aabbcc
You do not need a character class, you just have to mask it if you use single quotes in JavaScript.
Also, keep in mind that ' (U+0027) is different from ’ (U+2019) and must be handled separately.
A Kello's
str.replace(/[{\ },{\'}]/g,"");
result AKellos
I'm trying to match #(\w+) in a div content and remove it.
Here's what i've tried : http://jsfiddle.net/mxgde6m7/1/ .
#(\w+) works , but it doesn't replace with space.
var content = document.getElementById('contentbox');
var find = '#(\w+)';
var reg = new RegExp(find, 'g');
var result = content.innerHTML.replace(reg, ' ');
alert(result);
<div id="contentbox">#d test
What i want: <div id="contentbox">test
</div>
Thanks in advance.
EDIT
Okay, one problem solved, another one came up.
My script http://jsfiddle.net/mxgde6m7/9/ works perfectly there, but when i try it on my website, only a half works. The last part where it should replace #(\w+) with space doesn't work at all. If i copy/paste the CONTENT of the function in console(chrome), it works , but if i paste the function and i call it, it doesn't work.
Please help ! I'm stuck.
Using a RegExp constructor, you need two backslashes \\ in place of each backslash \.
var find = '#(\\w+)';
hwnd is correct that you need to double escape \w in your regular expression.
var find = '#(\\w+)';
But, you could also make this code much cleaner by defining a regex literal like so -
var content = document.getElementById('contentbox');
var result = content.innerHTML.replace(/#(\w+)/g, ' ');
alert(result);
Doing it this way doesn't require double escaping, as it's not a string.
My string is somewhat like this :
<rules>
<transTypeRuleList>
<transType type='stocks'>
<criteria result='401kContribution' priority='0'>
<field compare='contains' name='description'></field>
</criteria>
<criteria result='401kContribution' priority='1'>
<field compare='contains' name='description'></field>
</criteria>
</transType>
</transTypeRuleList>
</rules>
I need to take this and paste it in eclipse as a string , I know eclipse has an option to escape multi line strings and all, but that gives me the string as with "\n \r" Which I don't want .
My ideal string would be just the Double quotes and a + at the end of each line somewhat like this.
var res= "<rules>"+
"<acctTypeRuleList>"+
"<acctTypetype='stocks'>"+
"<criteriaresult='individual'priority='0'>"+
"<fieldcompare='contains'name='accountName'></field>"+
"</criteria>"+
"<criteriaresult='individual'priority='1'>"+
"<fieldcompare='contains'name='accountName'></field>"+
"</criteria>"+
"</acctType>"+
"</acctTypeRuleList>"+
"<transTypeRuleList>"+
"<transTypetype='stocks'>"+
"<criteriaresult='401kContribution'priority='0'>"+
"<fieldcompare='contains'name='description'></field>"+
"</criteria>"+
"<criteriaresult='401kContribution'priority='1'>"+
"<fieldcompare='contains'name='description'></field>"+
"</criteria>"+
"</transType>"+
"</transTypeRuleList>"+
"</rules>";
While preserving the indentation. So I am looking at regex.
So I guess finding ^(.*)$ and replacing with "$1" + should have done the job but it doesn't work .
Have a look at the fiddle. :
Link
Thanks in advance.
This seems to work:
http://regexr.com/38ps2
What I did
added the multiline m switch
removed the empty line in the text. Looks like regexr has problems with that.
I'd try with this:
'var res = "' + xml.replace(/\r?\n\s*/g, '" +\r\n\t"') + '";';
But remember that Javascript does allow multiline strings. Just put a backslash at the end of each line:
"<rules>\
<transTypeRuleList>\
...\
</rules>";