problem using regular expressions with javascript - 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/

Related

how to make a regexp in js split to break using special text characters

I need help to make this work in JS:
a="casa-me,pois estou farto! Eis a lista:uma;duas;três."
a.split(/regex/) to return
a=["casa","me","pois","estou","farto","Eis","a","lista","uma","duas","três"]
Thank you.
Other answers seem to miss things like accented characters, etc. This seems to be working well against your test string, though:
var re = /[^A-zÀ-ÿ]+/g;
var str = 'casa-me,pois estou farto! Eis a lista:uma;duas;três';
var result = str.split(re);
alert(result);
here you go:
a.match(/\w+/g).join(' ');
What you want is a global matching regex (note the g modifier):
yourString.replace(/[,;:.!]/g, ' ')
Regexp is something very nice.
var a = 'casa-me,pois estou farto! Eis a lista:uma;duas;três.';
var array = a.match(/[àáâãèéêẽìíîĩòóôõùúûũ\w]+/g);
Each array position will have a word as you want.

Placing variable in JavaScript regex

I've got a regular expression like this one:
var reg = /http:\/\/s(\d+)\.de\.example\.com\/(.*?)\.php(.*)/i;
I want to have a variable, exactly this: ccode[i][2] in place of .de.
How do I do that? Thank for your help.
You need to use a RegExp constructor notation if you want to use variables in your regex pattern, and inside it, you need to double escape special regex metacharacters. I.e.
var reg = new RegExp("http://s(\\d+)\\." + ccode[i][2] + "\\.example\\.com/(.*?)\\.php(.*)", "i");
From MDN:
Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.
Sample code:
var ccode = "de";
var reg = new RegExp("http://s(\\d+)\\." + ccode + "\\.example\\.com/(.*?)\\.php(.*)", "i");
alert(reg.test("http://s123.de.example.com/something.php?some=params"));
Try this
var reg = '/http:\/\/s(\d+)\\'+ccode[i][2]+'\.example\.com\/(.*?)\.php(.*)/i';

RegEx for WordPress file

I am trying to recognize a string like this one: file=2013/08/something_320x480.jpg and to replace it in JavaScript.
Here is my regex:
newStr = str.replace('/file=\d+\/\d+\/.+\d+x\d+.jpg/', 'irrelevant');
I also tried
newStr = str.replace('/file=.+\.jpg/', 'irrelevant');
However, my string is never replaced. What am I doing wrong?
The regexp literal does not take apostrophes.
Try:
newStr = str.replace(/file=\d+\/\d+\/.+\d+x\d+.jpg/, 'irrelevant');
Are you sure the file is set as you say it is? I just tried your example in the console and it works...
> var a = "file=2013/08/something_320x480.jpg"
undefined
> a.replace(/^file=\d+\/\d+\/.+\d+x\d+.jpg$/, 'irrelevant');
"irrelevant"
Update: I didn't spot that you had apos' in your regex, well spotted #Taemyr

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

How to replace all \" in a JS string?

How to replace all \" to " in a string?
I tried, but it doesn't works: var foobar = ("foo\\\"bar\\\"foo").replace(/"\\\""/,'"');
The result is foo\"bar\"foo , but it should be foo"bar"foo
You don't need to use quotes inside of a RegEx pattern, the // delimiters act as ones.
var foobar = "foo\\\"bar\\\"foo".replace(/\\"/g,'"');
Works for me.
Try .replace(/\\"/g,'"'); - regexes don't need quotes around them, I'm surprised you get any result at all.
You need to fix your regex, you need to do
replace(/\\\"/g, "\"")
Your quoting is wrong and you're not using g - global flag. It should be:
var foobar = ("foo\\\"bar\\\"foo").replace(/\\"/g,'"');
Try defining it like this
var foobar = ("foo\\\"bar\\\"foo").replace(/"\\\""/g,'"');
note that the .replace has a /g which makes it global
jsfiddle
// initial string
var str = "AAAbbbAAAccc";
// replace here
str = str.replace(/A/g, "Z");
alert(str);
​

Categories

Resources