Javascript regex replace single slash in to double slash? - javascript

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");

Related

How to remove forward and backward slashes from string in javascript

I want to remove all the forward and backward slash characters from the string using the Javascript.
Here is what I have tried:
var str = "//hcandna\\"
str.replace(/\\/g,'');
I also tried using str.replace(/\\///g,''), but I am not able to do it.
How can I do it?
You can just replace \/ or (|) \\ to remove all occurrences:
var str = "//hcandna\\"
console.log( str.replace(/\\|\//g,'') );
Little side note about escaping in your RegEx:
A slash \ in front of a reserved character, is to escape it from it's function and just represent it as a char. That's why your approach \\// did not make sense. You escapes \ with \, so it becomes \\. But if you want to escape /, you need to do it like this too: \/.
You want something more like this:
var str = "//hcandna\\"
str=str.replace(/[\/\\]/g,'');
console.log(str);
This will search for the set of characters containing a forward or backward slash and replace them globally. What you had requires a backslash followed by a forward slash.
Here's the output from Node:
str.replace(/[\/\\]/g,'')
'hcandna'
You need to add the result to a new string like:
var newstr = str.replace(/(\\|\/)+/ig, '');
You can use this code snippet
str.replace(/(\\|\/)/g,'');

replace double slash in javascript

I'm working in a javascript function, in a given string I need to replace // for only one slash / by now I have:
result= mystring.replace("\/\/", "/");
bt it's not working, I still get the string with double slash, so which is the proper regex to indicate the double slash to the replace function?
I already tried:
!//!
////
///g///g
Edit:
I'm using it to correct a URL that is saved in the string, for example,
sometimes that URL can be something like: mywebpage/someparameter//someotherparameter and that double slash gives problem, so I need to replace it to one single slash like: mywebpage/someparameter/someotherparameter
Use regex /\/\//(or /\/{2}/) with a global modifier to replace all occurrence.
result= mystring.replace(/\/\//g, "/");
console.log(
'hi// hello//123//'.replace(/\/\//g, '/')
)
There is no need to escape it if it is a string used as a replacement
console.log("asd//qwe".replace("//","/"));
If it were a regular expression, you would need to escape it
console.log("asd//qwe".replace(/\/\//,"/"));
Now if there is more than one set, than you need to use a regular expression with a global modifier.
console.log("asd//qwe".replace(/\/\//g,"/"));

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 -

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