How do I replace + with \+ using the replace method?
console.log("abc+pqr".replace("+","\+"));
This gives the original string abc+pqr but I want abc\+pqr.
How do I get this?
You need to escape the backslash:
console.log("abc+pqr".replace("+","\\+"));
Related
My string is tel:\\99999999999. How i can replace '\' to '\' single? I want output like: tel:\99999999999.
Please see attached image backslashes not showing into question.
You should escape the slash twice:
"tel:\\99999999999".replace('\\\\', '\\');
You can simply use tel.replace(/\\\\/g, "\\").
Demo:
var tel ="\\99999999999";
console.log(tel.replace(/\\\\/g, "\\"));
You need to escape the \ character with another \, because it's an escape character in JavaScript, you can check JavaScript backslash (\) in variables is causing an error for further reading.
use .replace() to achieve what you want, str.replace("\\\\","\\")
var str = "tel:\\99999999999";
console.log(str.replace("\\\\","\\"))
You can do like this
"tel:\\99999999999".replace('\\\\', '');
I have the following problem with my regex.
I want to search a string between two strings.
The datas is like that:
var datas = "a='00-8'b='13-'a+='00-2'b+='3333'c='4'";
I try:
datas.match("a\+='(.*?)'");
I can't get the regex working due to the + sign.
Any help ?
You're passing a String into match, not a RegExp, perhaps you wanted
datas.match(/a\+='(.*?)'/);
Alternatively, you need to escape your backslash for the String so it can escape the + as a RegExp, i.e.
datas.match("a\\+='(.*?)'");
Enclose the regex within forward slashes.
datas.match(/a\+='(.*?)'/g);
OR
Escape the backslash one more time, if it's enclosed within double quotes.
> datas.match("a\\+='(.*?)'");
[ 'a+=\'00-2\'',
'00-2',
index: 15,
input: 'a=\'00-8\'b=\'13-\'a+=\'00-2\'b+=\'3333\'c=\'4\'' ]
> datas.match("a\\+='(.*?)'")[1];
'00-2'
Javascript regex replace single slash into double slash not for replace double slash in a string?
var tempPath ="//DocumentImages//Invoices//USD//20130425//I27566554 Page- 1.tif&//hercimg/IMAGES/2008/20130411/16192144/16192144-10003.tif&";
Here replace all single slash in to double (//) not to all double slash.
like //DocumentImages//Invoices//USD//20130425//I27566554 Page- 1.tif&//hercimg//IMAGES//2008//20130411//16192144//16192144-10003.tif&
This would work assuming your string does not also end in a /
yourString.replace(/\/[^\/]/g,"//")
/stuff/ is just JavaScript regex literal notation
\/ is an escaped "/"
[^\/] is anything but a "/" (again, with escaping)
the "g" on the regex literal means "replace all matches and not just the first"
which we replace for "//" which is what you want.
replace accepts a string and returns a new string with the value changed without changing the original.
Here is a working fiddle
yourString.replace(/([^\/])\/([^\/])/g,"$1//$2")
Could be also helpful:
var s = "http://www.some-url.com//path//to";
var res = s.replace(/(https?:\/\/)|(\/)+/g, "$1$2");
I have seen all the questions asked previously but it didn't help me out . I have a string that contains backslash and i want to replace the backslashes with '-'
var s="adbc\sjhf\fkjfh\af";
s = s.replace(/\\/g,'-');
alert(s);
I thought this is the proper way to do it and of course i am wrong because in alert it shows adbcsjhffkjfhaf but i need it to be like adbc-sjhf-fkjfh-af.
What mistake i do here and what is the reason for it and how to achieve this...??
Working JS Fiddle
Your s is initially adbcsjhffkjfhaf. You meant
var s="adbc\\sjhf\\fkjfh\\af";
You need to double-up the backslashes in your input string:
var s="adbc\\sjhf\\fkjfh\\af";
Prefixing a character with '\' in a string literal gives special meaning to that character (eg '\t' means a tab character). If you want to actually include a '\' in your string you must escape it with a second backslash: '\\'
Javascript is ignoring the \ in \s \f \a in your string. Do a console.log(s) after assigning, you will understand.
You need to escape \ with \\. Like: "adbc\\sjhf\\fkjfh\\af"
The string doesn't contain a backslash, it contains the \a, \s and \f (escape sequence for Form Feed).
if you change your string to adbc\\sjhf\\fkjfh\\af
var s="adbc\\sjhf\\fkjfh\\af";
s = s.replace(/\\/g,'-');
alert(s);
you will be able to replace it with -
I want to use the pattern *1*. I have tried \*1\*, but it doesn't work. Where is the problem?
You have to escape it with a backslash:
/\*1\*/
Otherwise, an unescaped * in a RegExp will mean: Match 0 or more of the Preceding Character Group.
Update:
If you use the RegExp constructor, do it this way:
new RegExp("\\*1\\*")
You have to double-escape the backslashes because they need to be escaped in the string itself.
need to use a backslash \ as the escape character in regexes.