javascript replace and slashes - javascript

I've got a line to flip all slashes in a string and it works great.
**flipSlashes = shortLocString.replace(/\\/g,"/");**
But if I want to flip them back, it falls apart. I've tried all of the following. I don't "get" the //g syntax very clearly, so don't know how to troubleshoot it.
**flipSlashes = shortLocString.replace(///g,"\\");**
**flipSlashes = shortUrlString.replace(/'/'/g,"\\");**
**flipSlashes = shortUrlString.replace(///g,"\\");**
Any help appreciated,
dp

use (i.e. / in regex must be escaped using \/)
flipSlashes = shortLocString.replace(/\//g,"\\");

This will give you both ways
var shortLocString = "a/b//c/d//e///f////";
var shortLocString1 = shortLocString.replace(/\//g,"\\");
var shortLocString2 = shortLocString1.replace(/\\/g,"/");

Related

I am trying to figure out a regex in JavaScript for removing any strings before the dot [duplicate]

This question already has answers here:
How can I get file extensions with JavaScript?
(36 answers)
Closed 7 years ago.
I am looking for some help with Regex syntax. I tried many ways to make the syntax for this case, but nothing seems to work. Could someone help me with this?
So I have a case where I am trying to replace everything before the "." symbol in this string,
example_ so.myname
I successfully removed all the strings before the "." using this expression:
var removeStrBeforeDot = params.value.replace(/(\<b\>|\<nobr\>)[a-z]*\./g, "");
removeStrBeforeDot = removeStrBeforeDot.replace(/\(.*\)\<\/b\>/g, " ");
But the issue with this expression is, this works when there is only letters before the dot and doesn't work when there are symbols like , space, etc. Like for example, example so.myname has a space and a "_" which is not working in this case. What I like to do is keep it more generic and remove any string before the "." to be removed. Any help could be greatly appreciable.
Thanks!
One solution :
var str = 'example_ so.myname';
var result = str.replace(/[^.]*.(.*)/g , '$1');
console.log(result);
document.getElementById('el').innerHTML = result;
<div id='el'></div>
Another solution : most rapid, and less expensive !
var str = 'example_ so.myname';
var result = str.slice(str.indexOf('.')+1)
console.log(result);
document.getElementById('el').innerHTML = result;
<div id='el'></div>
This should work. It gets the position of the first dot and then collects the characters in the string from that point forward.
var dot = str.indexOf('.');
var afterDot = str.substr(dot+1);
Don't use a regex if you can do it with a simpler approach.
You can do like this:
st = 'example_ so.myname';
re = str.replace(/^.*\.+/g , '');
console.log(re)
I guess split is probably the easiest way:
alert("example_ so.myname".split(/\./)[1]);

Match #(\w+) and replace in javascript

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.

Get portion of a string using Javascript

I have a string (from the pathname in the url) and I am trying to pull out part of it, but I'm having trouble.
This is what I have so far:
^(/svc_2/pub/(.*?).php)
The string is:
/svc_2/pub/stats/dashboard.php?ajax=1
How can I get a regex that returns /pub/stats/dashboard only?
If it's always that format (I'm assuming the /svc_2/ is always there) this should do it.
var s = "/svc_2/pub/stats/dashboard.php?ajax=1";
var match = s.match(/\/svc_2(.+)\./)[1];
But not if anything comes before that.
For this, using a regex is too much, but here is:
var string = "/svc_2/pub/stats/dashboard.php?ajax=1";
console.log(string.replace(/.*(\/pub.*)\.php.*$/,"$1"));
But you can do it, without a regex, like this
console.log(string.substr(6,string.indexOf(".php")-6));
In both cases, the console.log will give you /pub/stats/dashboard
This is not very flexible, but should work in this specific case.
var basedir = '/svc_2/', str = '/svc_2/pub/stats/dashboard.php?ajax=1';
str = str.substring(basedir.length, str.indexOf('.'));
alert(str);

Inverse regex javascript

I'm trying to remove everything in a string that does not match 'standard' characters. Heres what I have so far:
var result = myString.replace(/^(?![A-Za-z0-9]+)/g, '');
Which does not work. Can someone point to me what I'm not doing right?
I think you mean this:
var result = myString.replace(/[^a-z0-9]/gi,'');

javascript split() of a attribute src of an iframe?

I have this:
<iframe src="http://999.999.99.99/reporting/report.php?rid=2&gid=6&ftid=0&fid=1000&gty=1&ltid=1&lid=1000&mode=7&width=220&sid=0&cc=666666,ffa401">
I tried split() in getting the value between rid= and & but it does not get what I wanted. I only wanted the value between those two. Please help.
This is my code so far:
var source = 'http://999.999.99.99/reporting/report.php?rid=2&gid=6&ftid=0&fid=1000&gty=1&ltid=1&lid=1000&mode=7&width=220&sid=0&cc=666666,ffa401';
var rid = source.split('rid= &').pop();
alert(rid);
Using a URL parser described here.
var parts = getUrlParts(url);
var rid = parts.rid;
Use a regular expression
var str="http://999.999.99.99/reporting/report.php?rid=2&gid=6&ftid=0&fid=1000&gty=1&ltid=1&lid=1000&mode=7&width=220&sid=0&cc=666666,ffa401";
var re = /[?&]rid=([^&]+)/i;
var part = str.match(re);
console.log(part[1]);
Consider using a simple regular expression:
var rid = src.match(/rid=(\d+)/)[1];
You could match against a regular expression, but if you want to do that with split, try:
var a = src.split("rid=")[1].split("&")[0];
You can use this code:
var rid = src.substr(src.indexOf('rid') + 4).split('&')[0];
Thanks a lot guys, I can really count on you guys here in SO.
So far, from your answers, I have used match() and it worked fine. Really, thank you guys. :)

Categories

Resources