I've been trying to create a regex which would ignore the casing.
This is the regex i am trying to use:
/^[A-Za-z0-9._+\-\']+#+test.com$/;
So basically i would want to match any of these
abc#Test.com
abc#TEST.com
abc#teSt.com
I tried this, but it doesn't work:
/^[A-Za-z0-9._+\-\']+#+(?i)+test.com$/;
I read somewhere about the use of (?i), but couldn't find any examples which show their usage in regex to ignore casing.
Thoughts anyone ? Thanks a lot in advance.
Flags go at the end.
/regex/i
i is for case-Insensitive (or ignore-case)
For anyone else who arrives here looking for this, if you've got code that is using the RegExp constructor you can also do this by specifying flags as a second argument:
new RegExp(pattern[, flags])
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp
For example:
var pattern = /^[A-Za-z0-9._+\-\']+#+test.com$/;
var regExp = new RegExp(pattern, "i");
Simple and straightforward, use following regex expression.
(?i)^[A-Za-z0-9._\-\']+#test.com$
Related
I really like Regular Expressions. I was wondering if this kind of things can be done in JavaScript.
Imagine that you want to match the words foo and bar, but only when they are followed by "?" Or "!". I know that this simple case could be done with x(?=y), but my intention here is to know if you can place one RegEx inside another, as it would be great for more difficult scenarios.
I mean something like this:
var regex1 = /foo|bar/;
var regex2 = /{regex1}[\?\!]/;
This would simplify very complex regexes and would make some patters reusable without having to write the same patterns every time you need it. It would be something like a RegEx variable, if that makes sense.
Is it possible?
Use RegExp constructor.
new RegExp('(?:' + regex1.source + ')[?!]')
regex1.source will give the actual regex as string without the delimiters and flags which then can be passed to the RegExp constructor to create new regex.
Also, note that there is no need to escape ? and ! when used inside character class.
var regex1 = /foo|bar/;
var regex = new RegExp('(?:' + regex1.source + ')[?!]')
console.log(regex);
Need help getting the following JavaScript RegEx case insensitive:
^(ABCDE)\d{5}$
I have tried /i but it doesn't work:
^(ABCDE)\d{5}$/i
Where should I place /i to get it to work?
Thanks in advance.
When you have a literal regex notation, just use /.../:
var re = /^(ABCDE)\d{5}$/i;
If you use a RegExp constructor:
var re = RegExp("^(ABCDE)[0-9]{5}$", "i");
However, the literal notation is preferable here since the pattern is constant, known from the beginning, and no variables are used to build it dynamically. Note that if you were to use \d in the RegExp constructor, you'd have to double the backslashes:
var re = RegExp("^(ABCDE)\\d{5}$", "i");
Try to write it this way :
var regex = /^(ABCDE)\d{5}$/i;
It needs the first / or you can also use
var regex = new RegExp('^(ABCDE)\\d{5}$', 'i');
Then if you try in a console this it should work (online testers can add other issue, just try directly on your code) :
regex.test('ABCDE12345') // true
regex.test('abcde12345') // true
Test on Regex101 : https://regex101.com/r/zR5yR0/1
I want to replace all occurences of .digit with 0.digit.
I'm new to regular expressions but as far as I understand I could use look behind to do this. But JS does not support that, I'd like to know if someone knows a solution.
To show the problem I wrote the following code.
str = "0.11blabla.22bla0.33bla.33"
allow = "\\.\\d*"
str.match(new RegExp(allow,"g"))
[".11", ".22", ".33", ".33"]
deny = "0\\.\\d*"
str.match(new RegExp(deny,"g"))
["0.11", "0.33"]
diffreg= new RegExp("(?!"+deny+")"+allow,"g") // translates to: /(?!0\.\d*)\.\d*/g
str.match(diffreg)
[".11", ".22", ".33", ".33"]
Obviously allow matches all decimal values whereas deny matches all values with a preceding 0. The result should of course be the set difference between the two: [".33", ".33"].
Use a group match.
> str.replace(/([^0])(\.\d)/g, "$10$2");
"0.11blabla0.22bla0.33bla0.33"
I think you are looking for this regex instead
[0]?(\.\d*)
So in your code you will have:
intersectionreg = new RegExp("[0]?("+allow+")","g")
Thanks #richard, edited
I am desperate - I don't see what I'm doing wrong. I try to replace all occurrences of '8969' but I always get the original string (no matter whether tmp is a string or an int). Maybe it's already too late, maybe I'm blind, ...
var tmp = "8969";
alert("8969_8969".replace(/tmp/g, "99"));
Can someone help me out?
The / characters are the container for a regular expression in this case. 'tmp' is therefore not used as a variable, but as a literal string.
var tmp = /8969/g;
alert("8969_8969".replace(tmp, "99"));
alert("8969_8969".replace(/8969/g, "99"));
or
var tmp = "8969"
alert("8969_8969".replace(new RegExp(tmp,"g"), "99"));
Live DEMO
Dynamic way of handling a regex:
var nRegExp = new RegExp("8969", 'g');
alert("8969_8969".replace(nRegExp, "99"));
/tmp/g. This is a regex looking for the phrase "tmp". You need to use new RegExp to make a dynamic regex.
alert("8969_8969".replace(new RegExp(tmp,'g'), "99"));
Javascript doesn't support that usage of tmp, it will try to use 'tmp' literally, as a regex pattern.
"8969_8969".replace(new RegExp(tmp,'g'), "99")
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];