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,"/"));
Related
The regex allows chars that are: alphanumeric, space, '-', '_', '&', '()' and '/'
this is the expression
[\s\/\)\(\w&-]
I have tested this in various online testers and know it works, I just can't get it to work correctly in code. I get sysntax errors with anything I try.. any suggestions?
var programProductRegex = new RegExp([\s\/\)\(\w&-]);
You can use the regular expression syntax:
var programProductRegex = /[\s\/\)\(\w&-]/;
You use forward slashes to delimit the regex pattern.
If you use the RegExp object constructor you need to pass in a string. Because backslashes are special escape characters inside JavaScript strings and they're also escape characters in regular expressions, you need to use two backslashes to do a regex escape inside a string. The equivalent code using a string would then be:
var programProductRegex = new RegExp("[\\s\\/\\)\\(\\w&-]");
All the backslashes that were in the original regular expression need to be escaped in the string to be correctly interpreted as backslashes.
Of course the first option is better. The constructor is helpful when you obtain a string from somewhere and want to make a regular expression out of it.
var programProductRegex = new RegExp(userInput);
If you are using a String and want to escape characters like (, you need to write \\( (meaning writing backslash, then the opening parenthesis => escaping it).
If you are using the RegExp object, you only need one backslash for each character (like \()
Enclose your regex with delimiters:
var programProductRegex = /[\s\/)(\w&-]/;
Scenario
I want to extract the path string from the document.location, excluding the leading slash.
So for example, if the url is:
http://stackoverflow.com/questions/ask
I would get:
questions/ask
This should be straightforward:
/* group everything after the leading slash */
var re = /\/(.+)/gi;
var matches = document.location.pathname.match(re);
console.log(matches[0]);
But if I run this snippet in the firebug console, I still get the leading slash.
I have already tested the regexp, and the regexp engine correctly extract the group.
Question
How to properly get the group 1 string?
You don't really need regular expressions if you just want to get pathname without leading slash. Since location.pathname always starts with / you can simply take the substring from the first index:
document.location.pathname.substr(1) // or .slice(1)
Are you saying trailing or leading slash? From your post it looks like leading slash.
document.location.pathname.replace(/^\//,"")
By the way, your regexp is right, but you just need to remove gi and read matches[1] rather than matches[0], because matches[0] is the whole string matches the regexp, while matches[1] is the captured part within the matched string (quote with brackets in the regexp).
var matches = document.location.pathname.match(/\/(.+)/);
console.log(matches); // ["/questions/ask", "questions/ask"]
Using regex you can do:
var m = 'http://stackoverflow.com/questions/ask'.match(/\/{2}[^\/]+(\/.+)/);
console.log(m[1]); /questions/ask
I have string look like this :
"fdsgsgf.signature=xxxxx(bv)"
And i want to get xxxxx
With : var testRE = html.match(".signature=(.*)/\(");
And when i run it i get exception that it's not valid regex.
Any idea why?
Some issues with your code:
You're missing starting slash / of your regex
Instead of .* you should better use [^(]+
dot needs to be escaped
Modified code:
html.match(/\.signature=([^(]+)/);
You need to double escape the backslash: ".signature=(.*)/\\(". This is a valid regex, but it will match the / char though. If you don't need it, simply remove it ;)
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");
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"))