Anyone help? When I run this I get " invalid quantifier ?<=href= "
var aHrefMatch = new RegExp("(?<=href\=")[^]+?(?=")");
var matchedLink = mystring.match(aHrefMatch);
But I know the regular expression is valid.
Any ideas?
Javascript does not support lookbehind assertions. It only supports lookahead ones. The error is produced because it assumes the ? is a quantifier for 0 or 1, but there is no element to quantify at the beginning of a subpattern (started by that ( opening parenthesis)
Also, your string seems to be missing a few backslashes, as the double quotes are not escaped there. It should produce a syntax error.
Perhaps this code could help you do what you are trying to achieve:
var match = mystring.match(/href=\"([^\"]*)\"/);
var matchedLink = match[1];
You need to escape the double quotes in the regular expression with the standard backslash:
var aHrefMatch = new RegExp("(?<=href\=\")[^]+?(?=\")");
...or you could just use single quotes to specify the string:
var aHrefMatch = new RegExp('(?<=href\=")[^]+?(?=")');
Did you mean to escape the quote after the = sign and after the look ahead ?=.
Also if you are just trying to match the href="some text" , then you really don't need look behind and look ahead constructs. The following should do just fine
href=\"[^"]+\"
If you are trying to match something else, please elaborate. Thanks
Don't really know what you want to do. But if you want to get the link.
var aHrefMatch = new RegExp(/(href\=\")([\w\-\/]+)?(\")/);
var matchedLink = mystring.match(aHrefMatch)[2];
Related
In Javascript, I want to match a string pattern that goes something like:
\left((some expression here to be matched)\right)
It's a little more complicated than that, so I have to define my pattern using the RegExp constructor. The simplified version is:
var pattern_string = '\\left\\((' + EXPRESSIONpattern + ')\\\\right\\)' ;
var pattern_regexp = new RegExp(pattern_string, 'g');
I realize that \r is the carriage return character in a string, hence having the four back slashes in the pattern_string before the r. Upon implementing, the only way I could get it to work was to also treat \l as a special string character and use:
var pattern_string = '\\\\left\\((' + EXPRESSIONpattern + '\\\\right\\)' ;
var pattern_regexp = new RegExp(pattern_string, 'g');
Why do I need to double escape the l? I can't find a reference that says \l is a special string sequence. What does \l mean in a string? Can someone point me to a reference that includes all characters that need to be double escaped in a string? It would help greatly with debugging to know for sure when I need to double escape.
Thanks in advance for your help,
T
I'm trying to replace multiple occurrences of a string and nothing seems to be working for me. In my browser or even when testing online. Where am I going wrong?
str = '[{name}] is happy today as data-name="[{name}]" won the match today. [{name}] made 100 runs.';
str = str.replace('/[{name}]/gi','John');
console.log(str);
http://jsfiddle.net/SXTd4/
I got that example from here, and that too wont work.
You must not quote regexes, the correct notation would be:
str = str.replace(/\[{name}\]/gi,'John');
Also, you have to escape the [], because otherwise the content inside is treated as character class.
Updating your fiddle accordingly makes it work.
There are two ways declaring regexes:
// literal notation - the preferred option
var re = /regex here/;
// via constructor
var re = new Regexp('regex here');
You should not put your regex in quotes and you need to escape []
Simply use
str = str.replace(/\[{name}\]/gi,'John');
DEMO
While there are plenty of regex answers here is another way:
str = str.split('[{name}]').join('John');
The characters [ ] { } should be escaped in your regular expression.
I've a string done like this: "http://something.org/dom/My_happy_dog_%28is%29cool!"
How can I remove all the initial domain, the multiple underscore and the percentage stuff?
For now I'm just doing some multiple replace, like
str = str.replace("http://something.org/dom/","");
str = str.replace("_%28"," ");
and go on, but it's really ugly.. any help?
Thanks!
EDIT:
the exact input would be "My happy dog is cool!" so I would like to get rid of the initial address and remove the underscores and percentage and put the spaces in the right place!
The problem is that trying to put a regex on Chrome "something goes wrong". Is it a problem of Chrome or my regex?
I'd suggest:
var str = "http://something.org/dom/My_happy_dog_%28is%29cool!";
str.substring(str.lastIndexOf('/')+1).replace(/(_)|(%\d{2,})/g,' ');
JS Fiddle demo.
The reason I took this approach is that RegEx is fairly expensive, and is often tricky to fine tune to the point where edge-cases become less troublesome; so I opted to use simple string manipulation to reduce the RegEx work.
Effectively the above creates a substring of the given str variable, from the index point of the lastIndexOf('/') (which does exactly what you'd expect) and adding 1 to that so the substring is from the point after the / not before it.
The regex: (_) matches the underscores, the | just serves as an or operator and the (%\d{2,}) serves to match digit characters that occur twice in succession and follow a % sign.
The parentheses surrounding each part of the regex around the |, serve to identify matching groups, which are used to identify what parts should be replaced by the ' ' (single-space) string in the second of the arguments passed to replace().
References:
lastIndexOf().
replace().
substring().
You can use unescape to decode the percentages:
str = unescape("http://something.org/dom/My_happy_dog_%28is%29cool!")
str = str.replace("http://something.org/dom/","");
Maybe you could use a regular expression to pull out what you need, rather than getting rid of what you don't want. What is it you are trying to keep?
You can also chain them together as in:
str.replace("http://something.org/dom/", "").replace("something else", "");
You haven't defined the problem very exactly. To get rid of all stretches of characters ending in %<digit><digit> you'd say
var re = /.*%\d\d/g;
var str = str.replace(re, "");
ok, if you want to replace all that stuff I think that you would need something like this:
/(http:\/\/.*\.[a-z]{3}\/.*\/)|(\%[a-z0-9][a-z0-9])|_/g
test
var string = "http://something.org/dom/My_happy_dog_%28is%29cool!";
string = string.replace(/(http:\/\/.*\.[a-z]{3}\/.*\/)|(\%[a-z0-9][a-z0-9])|_/g,"");
I'm new to regular expressions.
The following code works as expected, printing first "true" and then "false", the backslash in front of the period escaping it:
var pattern = new RegExp(/\./);
document.write(pattern.test("."));
document.write(pattern.test("a"));
But why does the following print "false":
var pattern = new RegExp(/\b\./);
document.write(pattern.test("."));
The period is, after all, at the beginning of the string.
You want to try using ^ -
/^\./
If you have
/\b\./
it matches the .'s in Hello. How are you.
It doesn't work because to have a word break, you first need to have a word.
Using a \b, this would work:
var pattern = new RegExp(/a\b\./);
document.write(pattern.test("a."));
If all you're doing is testing the first character, you can do it without a regex if you'd like.
".".charAt(0) === "."
I'm trying to write a regex for use in javascript.
var script = "function onclick() {loadArea('areaog_og_group_og_consumedservice', '\x26roleOrd\x3d1');}";
var match = new RegExp("'[^']*(\\.[^']*)*'").exec(script);
I would like split to contain two elements:
match[0] == "'areaog_og_group_og_consumedservice'";
match[1] == "'\x26roleOrd\x3d1'";
This regex matches correctly when testing it at gskinner.com/RegExr/ but it does not work in my Javascript. This issue can be replicated by testing ir here http://www.regextester.com/.
I need the solution to work with Internet Explorer 6 and above.
Can any regex guru's help?
Judging by your regex, it looks like you're trying to match a single-quoted string that may contain escaped quotes. The correct form of that regex is:
'[^'\\]*(?:\\.[^'\\]*)*'
(If you don't need to allow for escaped quotes, /'[^']*'/ is all you need.) You also have to set the g flag if you want to get both strings. Here's the regex in its regex-literal form:
/'[^'\\]*(?:\\.[^'\\]*)*'/g
If you use the RegExp constructor instead of a regex literal, you have to double-escape the backslashes: once for the string literal and once for the regex. You also have to pass the flags (g, i, m) as a separate parameter:
var rgx = new RegExp("'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'", "g");
while (result = rgx.exec(script))
print(result[0]);
The regex you're looking for is .*?('[^']*')\s*,\s*('[^']*'). The catch here is that, as usual, match[0] is the entire matched text (this is very normal) so it's not particularly useful to you. match[1] and match[2] are the two matches you're looking for.
var script = "function onclick() {loadArea('areaog_og_group_og_consumedservice', '\x26roleOrd\x3d1');}";
var parameters = /.*?('[^']*')\s*,\s*('[^']*')/.exec(script);
alert("you've done: loadArea("+parameters[1]+", "+parameters[2]+");");
The only issue I have with this is that it's somewhat inflexible. You might want to spend a little time to match function calls with 2 or 3 parameters?
EDIT
In response to you're request, here is the regex to match 1,2,3,...,n parameters. If you notice, I used a non-capturing group (the (?: ) part) to find many instances of the comma followed by the second parameter.
/.*?('[^']*')(?:\s*,\s*('[^']*'))*/
Maybe this:
'([^']*)'\s*,\s*'([^']*)'