I'm using this RexExp code var userIdPattern = new RegExp('^([A-z0-9_.]{4,15})$');, I want to check if last character was dot (.) then .test() returns me false:
var userIdPattern = new RegExp('^([A-z0-9_.]{4,15})$');
console.log(userIdPattern.test('Omid.my.')); // -> I need this to be false
and in this case return me true:
userIdPattern.test('Omid.my'); //-> true
Following the update, a more appropriate regex might be:
var userIdPattern = new RegExp('^([A-Za-z0-9\[\]\\^`][A-z0-9_.]{2,13}[A-Za-z0-9\[\]\\^`])$');
That is, if you want to include other special characters in the usernames like 7stud mentioned in his comment and only exclude . and _ from the first and last characters.
Otherwise, to prevent those characters, I would suggest:
var userIdPattern = new RegExp('^([A-Za-z0-9][A-Za-z0-9_.]{2,13}[A-Za-z0-9])$');
Fiddle to test.
You can make it like this
^([A-z0-9_.]{3,14}[A-z0-9])$
Edit after reading your comment
^[a-z0-9][a-z0-9_.]{2,13}[a-z0-9]$
Preview
Also I suggest you use flag the i to ignore case:
var userIdPattern = new RegExp('^[a-z0-9][a-z0-9_.]{3,13}[a-z0-9]$', 'i');
Related
Here is my string:
Organization 2
info#something.org.au more#something.com market#gmail.com single#noidea.com
Organization 3
headmistress#money.com head#skull.com
Also this is my pattern:
/^.*?#[^ ]+|^.*$/gm
As you see in the demo, the pattern matches this:
Organization 2
info#something.org.au
Organization 3
headmistress#money.com
My question: How can I make it inverse? I mean I want to match this:
more#something.com market#gmail.com single#noidea.com
head#skull.com
How can I do that? Actually I can write a new (and completely different) pattern to grab expected result, but I want to know, Is "inverting the result of a pattern" possible?
No, I don't believe there is a way to directly inverse a Regular Expression but keeping it the same otherwise.
However, you could achieve something close to what you're after by using your existing RegExp to replace its matches with an empty string:
var everythingThatDidntMatchStr = str.replace(/^.*?#[^ ]+|^.*$/gm, '');
You can replace the matches from first RegExp by using Array.prototype.forEach() to replace matched RegExp with empty string using `String.ptototype.replace();
var re = str.match(/^.*?#[^ ]+|^.*$/gm);
var res = str;
re.forEach(val => res = res.replace(new RegExp(val), ""));
here is the format of my regular expression:
#"^PR[a-zA-Z0-9-]{36}[0-9]{2}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$".
There should be separate validation for each condition. So I succeeded for first three conditions using JavaScript sub-string. Just stuck for last condition i.e.
"([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}".
In this, I want to check every fourth character must be "2".
How do i achieve this by JavaScript?
If you have the full regex, why not just use it as a whole:
var regex = /^PR[a-zA-Z0-9-]{36}[0-9]{2}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/;
var str = "PR12345678901234567890123456789012345600AAA2BBBCCCDDDEEEaaa2bbbcccdddeee";
console.log(regex.test(str)); // true
But if you must really break it, you could do:
var regex1 = /^PR[a-zA-Z0-9-]{36}$/;
console.log(regex1.test(str.substring(0,38))); // true
var regex2 = /^[0-9]{2}$/;
console.log(regex2.test(str.substring(38,40))); // true
var regex3 = /^([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/;
console.log(regex3.test(str.substring(40,str.length))); // true
See demo fiddle here.
Edit:
To check whether every fourth character must be two, use:
var regexfourth = /^([a-zA-Z0-9-]{3}2)+$/;
console.log(regexfourth.test("aaa2bbb2ccc2ddd2")); // true
console.log(regexfourth.test("aaa2bbb2ccc3ddd2")); // false(notice a 3 after ccc)
Demo fiddle for this here (check the bottom).
I am trying to validate year using Regex.test in javascript, but no able to figure out why its returning false.
var regEx = new RegExp("^(19|20)[\d]{2,2}$");
regEx.test(inputValue) returns false for input value 1981, 2007
Thanks
As you're creating a RegExp object using a string expression, you need to double the backslashes so they escape properly. Also [\d]{2,2} can be simplified to \d\d:
var regEx = new RegExp("^(19|20)\\d\\d$");
Or better yet use a regex literal to avoid doubling backslashes:
var regEx = /^(19|20)\d\d$/;
Found the REAL issue:
Change your declaration to remove quotes:
var regEx = new RegExp(/^(19|20)[\d]{2,2}$/);
Do you mean
var inputValue = "1981, 2007";
If so, this will fail because the pattern is not matched due to the start string (^) and end string ($) characters.
If you want to capture both years, remove these characters from your pattern and do a global match (with /g)
var regEx = new RegExp(/(?:19|20)\d{2}/g);
var inputValue = "1981, 2007";
var matches = inputValue.match(regEx);
matches will be an array containing all matches.
I've noticed, for reasons I can't explain, sometimes you have to have two \\ in front of the d.
so try [\\d] and see if that helps.
I have a string of text that I am trying to remove from a textarea in javascript. The text that I am trying to remove is in the following format:
Category:Attribute
Color:Green <- Example!
var catTitle = 'color';
var regexp = new RegExp(catTitle + '\:[.]*$', "g")
textarea.value = textarea.value.replace(regexp, "");
Since the attribute can be any length, I need my regular expression to go forward until the new line. I thought that [.]*$ would be acceptable to match any characters up to a new line; however, it doesn't seem to work.
Any help would be greatly appreciated.
. inside a character class ([]) matches itself -- it is no longer a wildcard. Also, regular expressions are case sensitive unless told otherwise ("i" flag) and : (in that context) is not special so it does not need to be escaped.
[a-z] or \w might be good to use to match the Attribute, depending.
It might then go like:
var catTitle = "color"
var re = new RegExp(catTitle + ':[a-z]+$', "gi")
re.test("Color:Green") // true
re.test("color:Red") // true
re.test("color: Red") // false, put perhaps this should be accepted?
re.test("color:") // false
re.test("foo:bar") // false
I also changed the qualifier from * to + -- note the case of "color:" and how it's rejected. This will also reject colors in non-English alphabets.
Happy coding.
Or, since you may have more lines:
var catTitle = 'Color';
var regexp = new RegExp(catTitle + ':.*', "g")
alert("skzxjclzxkc\nsasxasxColor:Red\nasdasdasd".replace(regexp, ""));
I am trying to create something similar to this:
var regexp_loc = /e/i;
except I want the regexp to be dependent on a string, so I tried to use new RegExp but I couldn't get what i wanted.
Basically I want the e in the above regexp to be a string variable but I fail with the syntax.
I tried something like this:
var keyword = "something";
var test_regexp = new RegExp("/" + keyword + "/i");
Basically I want to search for a sub string in a larger string then replace the string with some other string, case insensitive.
regards,
alexander
You need to pass the second parameter:
var r = new RegExp(keyword, "i");
You will also need to escape any special characters in the string to prevent regex injection attacks.
You should also remember to watch out for escape characters within a string...
For example if you wished to detect for a single number \d{1} and you did this...
var pattern = "\d{1}";
var re = new RegExp(pattern);
re.exec("1"); // fail! :(
that would fail as the initial \ is an escape character, you would need to "escape the escape", like so...
var pattern = "\\d{1}" // <-- spot the extra '\'
var re = new RegExp(pattern);
re.exec("1"); // success! :D
When using the RegExp constructor, you don't need the slashes like you do when using a regexp literal. So:
new RegExp(keyword, "i");
Note that you pass in the flags in the second parameter. See here for more info.
Want to share an example here:
I want to replace a string like: hi[var1][var2] to hi[newVar][var2].
and var1 are dynamic generated in the page.
so I had to use:
var regex = new RegExp("\\\\["+var1+"\\\\]",'ig');
mystring.replace(regex,'[newVar]');
This works pretty good to me. in case anyone need this like me.
The reason I have to go with [] is var1 might be a very easy pattern itself, adding the [] would be much accurate.
var keyword = "something";
var test_regexp = new RegExp(something,"i");
You need to convert RegExp, you actually can create a simple function to do it for you:
function toReg(str) {
if(!str || typeof str !== "string") {
return;
}
return new RegExp(str, "i");
}
and call it like:
toReg("something")