How to Remove double backslashes in a string to Single one? - javascript

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('\\\\', '');

Related

Javascript replace character with backslash

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("+","\\+"));

Regex need to includes the plus sign

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'

Replace '\' with '-' in a string

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 -

jQuery - Replace all parentheses in a string

I tried this:
mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace("(", "").replace(")", "");
It works for all double and single quotes but for parentheses, this only replaces the first parenthesis in the string.
How can I make it work to replace all parentheses in the string using JavaScript? Or replace all special characters in a string?
Try the following:
mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(|\)/g, "");
A little bit of REGEX to grab those pesky parentheses.
You should use something more like this:
mystring = mystring.replace(/["'()]/g,"");
The reason it wasn't working for the others is because you forgot the "global" argument (g)
note that [...] is a character class. anything between those brackets is replaced.
You should be able to do this in a single replace statement.
mystring = mystring.replace(/["'\(\)]/g, "");
If you're trying to replace all special characters you might want to use a pattern like this.
mystring = mystring.replace(/\W/g, "");
Which will replace any non-word character.
You can also use a regular experession if you're looking for parenthesis, you just need to escape them.
mystring = mystring.replace(/\(|\)/g, '');
This will remove all ( and ) in the entire string.
Just one replace will do:
"\"a(b)c'd{e}f[g]".replace(/[\(\)\[\]{}'"]/g,"")
That should work :
mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(/g, "").replace(/\)/g, "");
That's because to replace multiple occurrences you must use a regex as the search string where you are using a string literal. As you have found searching by strings will only replace the first occurrence.
The string-based replace method will not replace globally. As such, you probably want to use the regex-based replacing method. It should be noted:
You need to escape ( and ) as they are used for group matching:
mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(/g, "").replace(/\)/g, "");
This can solve the problem:
myString = myString.replace(/\"|\'|\(|\)/)
Example

JavaScript regex with escaped slashes does not replace

Do i have to escape slashes when putting them into regular expression?
myString = '/courses/test/user';
myString.replace(/\/courses\/([^\/]*)\/.*/, "$1");
document.write(myString);
Instead of printing "test", it prints the whole source string.
See this demo:
http://jsbin.com/esaro3/2/edit
Your regex is perfect, and yes, you must escape slashes since JavaScript uses the slashes to indicate regexes.
However, the problem is that JavaScript's replace method does not perform an in-place replace. That is, it does not actually change the string -- it just gives you the result of the replace.
Try this:
myString = '/courses/test/user';
myString = myString.replace(/\/courses\/([^\/]*)\/.*/, "$1");
document.write(myString);
This sets myString to the replaced value.
/[\/]/g matches forward slashes.
/[\\]/g matches backward slashes.
Actually, you don't need to escape the slash when inside a character class as in one part of your example (i.e., [^\/]* is fine as just [^/]*). If it is outside of a character class (like with the rest of your example such as \/courses), then you do need to escape slashes.
string.replace doesn't modify the original string. Instead, a returns a new string that has had the replacement performed.
Try:
myString = '/courses/test/user';
document.write(myString.replace(/\/courses\/([^\/]*)\/.*/, "$1"));
Note, that you don't have to escape / if you use new RegExp() constructor:
console.log(new RegExp("a/b").test("a/b"))

Categories

Resources