I have a javascript function that looks element id with certain patterns. So I have the following script:
if (f.elements[i].id.match(/DataList\[-\d{3}|\d{3}\]\.MemberId/)) {
//do something
}
It should match elements with ids such as these:
DataList[-1].MemberId
DataList[-2].MemberId
And it does, however it also matches the following:
DataList[-1].FirstName
DataList[-2].FirstName
which I don't want.
Could any guru take a look at the regular expression above and point out what is going wrong?
Thanks,
Cullen
Try to anchor your regex at the beginning with a ^ and at the end with a $, group your digit match and allow 1-3 digits instead of just 3.
if (f.elements[i].id.match(/^DataList\[(-\d{1,3}|\d{1,3})\]\.MemberId$/)) {
//do something
}
The way you had it, it was matching anything containing "DataList[-123" or containing "123].MemberId".
A simpler overall regex that accomplishes the same thing is:
if (f.elements[i].id.match(/^DataList\[-?\d{1,3}\]\.MemberId$/)) {
//do something
}
The or is saying:
DataList\[-\d{3} OR \d{3}\]\.MemberId/
This regex matches correctly:
DataList\[-?\d{1,3}\]\.MemberId
My suggestion
if (f.elements[i].id.match(/DataList\[-[0-9]{1,3}\]\.MemberId/)) {
}
The {} determines how many #s you want to support so 1-3 would match upu to [999]
Related
I'm trying to validate the value of an input text field with the following code:
function onBlurTexto(value) {
var regexNIT = "([a-zA-z]|[0-9]|[&#,#.ÑñáéíóúÁÉÍÓÚ\|\s])";
regexCompilado = new RegExp(regexNIT);
if (!(regexCompilado.test(value))) {
alert("Wrong character in text :(");
return false;
} else {
return true;
}
}
But when i enter this text:
!65a
the function returns true (as you can see, the "!" character does not exist in the regular expression)
I'm not an expert in regular expressions, so i think i am missing something in the building of this reg.exp.
How can i put this regular expression to work?
Thanks in advance.
EDIT
i am so sorry ... i should remove the references to the variable "regexpValidar" before posting the issue. I modified the sample. Thanks #TecBrat
You should provide the start (^) and end ($) flags to your regex. Now you are matching 65a since you have alternate sets.
This should work /^([a-zA-z]|[0-9]|[&#,#.ÑñáéíóúÁÉÍÓÚ\|\s])+$/g
Demo: https://regex101.com/r/zo2MpN/3
RegExp.test looks for a match in the string, it doesn't verify that the whole string matches the regex. In order to do the latter, you need to add start and end anchors to your regex (i.e. '^' at the start and '$' at the end, so you have "^your regex here$").
I also just noticed that your regex is currently matching only one character. You probably want to add a '+' after the parens so that it matches one or more:
"^([a-zA-z]|[0-9]|[&#,#.ÑñáéíóúÁÉÍÓÚ\|\s])+$"
This is wrong. the variable you use doesn't has anything. Try this instead.
var regexCompilado = new RegExp(regexNIT);
I'm trying to write a regular expression that matches filenames, such as:
image.png
image.svg
image.gif
However, I don't want it to match if my extension is preceded by .inline, like so:
image.inline.png
image.inline.svg
image.inline.gif
My current regular expression matches the first three, and is like so: /\.(gif|png|svg)$/ - however, I'm having trouble adding in the negative condition for a preceding .inline.
Thanks in advance for any advice.
I would also use a negative lookahead, but I would phrase it slightly differently:
var term = "image.inline.png";
var re = new RegExp("^(?!.*\\.inline\\.[^.]+$).*\\.(?:gif|png|svg)$");
if (re.test(term)) {
console.log("Valid");
} else {
console.log("Invalid");
}
You can use a negative lookahead to check for the preceeding .inline. See it in action here.
^\w+(?!\.inline)\.(gif|png|svg)$
I have this regex:
/(((\w+)|(\.\w+)|(\#\w+)|\*)(\[(.+(=".+"|\*".+"|\^".+"|))\])?(::|:)?)+(?=[ \S]*\{)/gm
Which I am trying to use to match CSS selectors. Consider this pseudo-code CSS input:
.main {
property: value;
}
.one, .two a[href$=".com"] {
.subclass {
property: value;
}
}
.test:before, .test:after:active {}
The pattern above will return the following matches:
['.body', '.one', '.two', 'a[href$=".com"]', '.subclass', '.test:before', '.test:after:active']
I am trying to modify the pattern so that psuedo selectors are not matched. So all the other matches should still be valid, but .test:before should just be .test and .test:after:active should also just match .test. I can't think of a way to do this without either a negative look-behind, or a way to not match if the first character is a :.
I'm implementing this in Node, and I don't want to lock my script to Node > 9.2.0 just to use negative look-behinds in my regex.
Any ideas would be greatly appreciated!
(?!.*:)(?:[.#]\w+|\w+\[.*\])
You could use something like this?
This uses a negative lookahead to ensure it doesn't capture anything with a colon beside it, as well as using a simplified version of your matcher for psuedo css elements.
See it working here
function(input){
return input.replace(/teststring/ig, "adifferentstring");
}
I want to replace "teststring" and "teststring\n" with "adifferentstring"
In regex, to match a specific character you can place it in brackets:
[\n]
To make the match "optional", you can follow it with a ?:
[\n]?
In your exact example, your full regex could be:
teststring[\n]?
So, your function would look like:
function replace(input) {
return input.replace(/teststring[\n]?/ig, "adifferentstring");
}
I'd suggest going with matching characters in brackets as this makes for easy expansion; consider, for instance, that you want to match Window's newlines (a carriage-return + a newline):
teststring[\r\n]?
Try
function(input){
return input.replace(/teststring\n?/ig, "adifferentstring");
}
Try .replace(/teststring[\n]?/ig,"adifferentstring");
It would be something like this:
var re = /teststring([\n]?)/ig;
So then your replace statement would look about like this:
return input.replace(re,"adifferentstring");
Here's a fiddle showing the regex works.
And then a fiddle showing the replace operation working.
Edit:
Actually, thinking about the problem a little further, if your regex does match a carriage return or new line character, that would need to get put back into the replacing string. The same regex I posted originally will work but you will need this replace statement instead (with the $1 denoting the first group in parantheses.
return input.replace(re,"adifferentstring$1");
fiddle
The value of product_id might be some combination of letters and numbers, like: GB47NTQQ.
I want to check to see if all but the 3rd and 4th characters are the same.
Something like:
if product_id = GBxxNTQQ //where x could be any number or letter.
//do things
else
//do other things
How can I accomplish this with JavaScript?
Use regular expression and string.match(). Periods are single wildcard characters.
string.match(/GB..NTQQ/);
Use a regular expression match:
if ('GB47NTQQ'.match(/^GB..NTQQ$/)) {
// yes, matches
}
Answers so far have suggested match, but test is likely more appropriate as it returns true or false, whereas match returns null or an array of matches so requires (implicit) type conversion of the result within the condition.
if (/GB..NTQQ/.test(product_id)) {
...
}
if (myString.match(/regex/)) { /*Success!*/ }
You can find more information here: http://www.regular-expressions.info/javascript.html