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

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. :)

Related

javascript replace and slashes

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

How do I change this value using regex?

I have a string that's structured as so:
"http://mydomain.com/?i=0"
I'd like to be able to change the value of i using a regular expression in javascript but I'm not sure how to do it. Is anyone able to help?
Here's a place to start:
str = "http://mydomain.com/?i=0";
str = str.replace(/((?:\?|&)i=)([^&]*)/g, '$1foo');
"http://mydomain.com/?i=0".replace(/i=.*$/, 'i='+<newValue>);
if you need to cater for additional parameters following i use
"http://mydomain.com/?i=0&j=k".replace(/i=[^&]+/, 'i='+<newValue>);
there is a fiddle to demonstrate it
Use the javascript replace() function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
I'm not going to do the Regex for you as you have demonstrated no attempt at doing it yourself.
Example:
var str = "http://mydomain.com/?i=0";
var i=1; // or any other value (e.g. i="test")
str = str.replace(/i=.*/, 'i='+i);
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 replace question mark

how to make a regex for ? and = in javascript?
I want something from
http://localhost/search?search=words
to
http://localhost/search/search/words
(?search=) to (/search/)
<script>
var ss = "http://localhost/search?search=words".replace("/\?search\=/g", "/search/");
document.write(ss);
</script>
BTW: just some prastic, not a htaccss rewrite. Thanks.
Almost there! = is not a special character and does not need to be escaped. In addition, regex strings are not wrapped by quotes. So:
"http://localhost/search?search=words".replace(/\?search=/g, "/search/");
How about
str.replace(/[?=]/g, "/");
Do note that it's probably better to make a function to understand the url structure and rebuild it properly, that will produce a much more healthy, robust code, rather then a simple replacement.
You can use a simple string for replace:
var ss = "http://localhost/search?search=words".replace("?search=", "/search/");

problem using regular expressions with javascript

I am using the following script but it's giving syntax errors which I am unable to figure out. Please help.
var str = "http://gaurav.com";
var patt1 = /^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$/;
console.log(str.match(patt1));
Thanks,
Gaurav
Needs to be in /'s
var patt1 = /^http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/;
Edit: also escape /s in the pattern.
You pattern gives a "," in the string, could that be the problem???
Try this:
var str = "http://gaurav.com";
var patt1 = 'http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}';
console.log(str.match(patt1));
See the working example here
You must escape all / as well:
var str = "http://gaurav.com";
var patt1 = /^http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/;
//------------------^-^---------------------------------^
console.log(str.match(patt1));
Quote the regexp string, that should fix the error. Haven't checked the expression, but that wasn't the question, right?
var patt1 = '^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$';
This seems to work just fine - looks like you're just missing your quotes
var str = "http://gaurav.com";
var patt1 = "^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$";
document.write(str.match(patt1));
Here's a jsfiddle link to the code you can play with http://jsfiddle.net/chuckplayer/fLrx8/

Categories

Resources