I'm trying to use a regular expression in JavaScript to match a number or a number containing a decimal. The regular expression looks like [0-9]+ | [0-9]* \. [0-9]+.
However, for some reason this '1A'.match(/^[0-9]+|[0-9]*\.[0-9]+$/) incorrectly finds a match. I'm not sure which part of the expression is matching the A.
The problem is your alternation. This is what it says:
^[0-9]+ # match an integer at the start
| # OR
[0-9]*\.[0-9]+$ # match a decimal number at the end
So the first alternative matches.
You need to group the alternation:
/^(?:[0-9]+|[0-9]*\.[0-9]+)$/
The ?: is an optimisation and a good habit. It suppresses capturing which is not needed in the given case.
You could get away without the alternation as well, though:
/^[0-9]*\.?[0-9]+$/
Or even shorter:
/^\d*\.?\d+$/
'1A'.match(/^[0-9]+|[0-9]*\.[0-9]+$/) finds a match because it is a union of:
^[0-9]+
and
[0-9]*\.[0-9]+$
where the first matches.
to avoid this, group them: ^([0-9]+|[0-9]*\.[0-9]+)$
and try this:
'1A'.match(/^([0-9]+|[0-9]*\.[0-9]+)$/) === null
alternatively:
function matchExacly(str, regex) {
var tmp = str.match(regex);
return tmp ? tmp[0] === str : false;
}
matchExacly('1A', /[0-9]+|[0-9]*\.[0-9]+/) === false
matchExacly('1', /[0-9]+|[0-9]*\.[0-9]+/) === true
Maybe I am at the wrong place but if you use regex just for validating numeric values, why not to use faster alternatives, as the following:
var isNumber = ( +n === parseFloat(n) );
Related
I need a regular expression for :
<<12.txt>> <<45.txt>
I have created a regular expression :
<<.+.txt>>
But this found one match in whole string but here is 2 matches:
<<12.txt>>
<<45.txt>>
if anyone have solution for this problem please help me out there
Part of the issue is that the string you've specified wouldn't match because the second > is missing in <<45.txt>.
Also, you're using the . (dot) selector, and also trying to find a period. It works, but now how you think it is.
Here's the regex you want:
var regex = /<<\d+\.txt>>/g
\d matches only numbers
\. matches an actual period
/g means global, so it won't stop at the first match
Practice Regular Expressions
https://regexr.com/43bs4
Demo
var string = "<<12.txt>> <<45.txt>>";
var regex = /<<\d+\.txt>>/g;
var matches = string.match(regex);
console.log(matches);
P.S., if you actually want to match with 1 > or 2 >>, you can with:
var regex = /<<\d+\.txt>>?/g
? optionally matches the character right before it
/<<.+.txt>>/gm
g is for global (will search through entire source)
m is for multi line search support
This seems simple enough but it's not working. I'm trying to match either an integer or the word "Other" (case insensitive). For example, given the array:
[1, 233, 45, "other", "the"]
Only the first 4 items in the array should be valid. However, currently my attempt will only match integers and is not capturing "other" (regardless of case insensitive).
This is the pattern and modifier that I'm using:
var regex = new RegExp("^[0-9]*|(other)$", "i"),
match = regex.exec(value);
return match && (match.index === 0) && match[0].length === value.length;
The idea is good but a few details need fixing :
var regex = /^(\d+|other)$/i;
It's not that it's capturing integers, it's capturing [0-9]*. The * means ZERO or more, so the empty string is valid. A + in it's place means one or more.
This is probably more general than you need, but numbers usually don't begin with a 0 except 0 itself, but if you don't mind allowing leading 0's, [0-9]+ will do. If you don't want to match numbers like 001, then you'll need something like 0|[1-9][0-9]*.
Also, the ^ and $ have greater precedence than |, which means you're actually matching anything starting with an empty string, or anything ending in 'other'
^(?:[0-9]+|other)$
Try this.See demo.
https://regex101.com/r/mS3tQ7/4
* and ? are the easiest for non-programmers to understand, with respect to wildcards; in this case, multiple characters and a single character wildcards, respectively.
If I receive a string, where a "?" could appear in any position of the string (e.g. "sing?" or "spo?ls"), how can I convert the string into a javascript regexp which I then compare against a dictionary list? In the case of "spo?ls", I would expect to match "spools", "spoils", etc.
Ditto for use of "*". Thanks.
Sorry if I was not clear: When I meant common wildcards, common to other environments, not Javascript: so, yes, "?" equals any single character [a-z] and "" equals one or more characters [a-z]. In the case of "", consider "*sing", which would match "arousing", "carousing", etc. Or, "ba*ed", which would match "baked", "banked", or "balanced".
Roughly:
* → .* (any character, zero or more)
? → . (any character, exactly one)
You'll also need to ignore case (with the i flag) and ensure nothing else matches (with anchors), e.g.:
spo?ls → /^spo.ls$/i
Whatever, I recommend you learn some basics about regular expressions. The MDN documentation is quite good.
P.S. The . metacharacter does not match new lines.
That isn't what ? does. ? is a quantifier, not a wild card.
If you want to match any single character, you need ..
In your case, you can replace all instances of ? with ., and then pass the string to RegExp:
pattern = "spo?ls";
// produces /spo.ls/
regex = RegExp(pattern.replace(/\?/g, '.'));
Ditto *: It's a quantifier, not a wild card. You can do the same for *, except you'd replace all instances of * with .*.
In regex the period (.) is used to match "any character".
So you could "convert" "spo?ls" to /spo.ls/.
If you wanted the . to possibly match 0 characters, you could use /spo.?ls/. The ? means "0 or 1 character".
In regex, the * character means "0 or more", and + means 1 or more.
So depending on what you were looking for, "spo*ls" could be converted to /spo.+ls/ or /spo.*ls/.
var string = 'spo?ls';
if (string.indexOf('?') !== -1) string = string.replace('?', '[a-zA-Z]');
if (string.indexOf('*') !== -1) string = string.replace('*', '[a-zA-Z]+');
var dictionary = ['spoils', 'spools', 'spoools', 'fools', 'tools'];
var re = new RegExp('^' + string + '$');
var results = dictionary.filter(function(el) {
return (el.match(re) !== null);
});
console.log(results);
["spoils", "spools"];
Now try with string = 'spo*ls';
console.log(results);
["spoils", "spools", "spoools"];
Pattern tokenpattern = Pattern.compile("string you get");
Matcher matcher = tokenpattern.matcher(content to check);
matcher.find();
Is this the answer to your question?
As i can see the String you get is already the RegularExpression and you just have to use it as one.
Lg Teifun2
I'm using this regex to validate float numbers:
var reg = /\d+\.?\d+/;
But it's validating this as true:
"11.34x"
"11.34abs"
"1a1.34abs"
The \d should only match numbers. What is happening?
If you don't anchor the regular expression, it will match a string that contains a substring that matches.
Try:
var reg = /^\d+\.?\d+$/;
The ^ matches the start of the test string, and $ matches the end. Thus that regular expression will only match strings that have nothing but digits and at most one ".".
edit — as pointed out, your use of the + quantifier means your regex requires digits; if there's a decimal point, then it requires digits on both sides. Maybe that's what you want, maybe it isn't.
use this regular expression ^\d+(\.\d+)?$
or ^\d+([.,]\d+)?$ separator can be comma or dot
Consider using the Number wrapper/constructor function instead:
Number('11.34'); // => 11.34
Number('11.34x'); // => NaN
[Edit] As commenter #VisioN points out, that function has an edge case for empty and pure-whitespace strings, so maybe create a wrapper function:
function reallyParseFloatingPointNumber(s) {
var s = (''+s).trim();
return (s==='') ? NaN : Number(s);
}
if (!"12 34.98 ".replace(/^\s+|\s+$/g,"").match(/^\d+\.?\d+$/))
alert("Please enter numbers in float form")
else alert ("Good Form, Old Chap!")
Alas, I am mistaken again! burning_LEGION is correct:
/^\d+(\.\d+)?$/
will match single digits too.
I was wondering if there is a way having this
var string = "foo::bar"
To get the last part of the string: "bar" using just regex.
I was trying to do look-aheads but couldn't master them enough to do this.
--
UPDATE
Perhaps some examples will make the question clearer.
var st1 = "foo::bar::0"
match should be 0
var st2 = "foo::bar::0-3aab"
match should be 0-3aab
var st3 = "foo"
no match should be found
You can use a negative lookahead:
/::(?!.*::)(.*)$/
The result will then be in the capture.
Another approach:
/^.*::(.*)$/
This should work because the .* matches greedily, so the :: will match the last occurence of that string.
Simply,
/::(.+)$/
You can't use lookaheads unless you know exactly how long a string you're trying to match. Fortunately, this isn't an issue, because you're only looking at the end of the string $.
I wouldn't use regular expressions for this (although you certainly can); I'd split the string on ::, since that's conceptually what you want to do.
function lastToken(str) {
var xs = str.split('::');
return xs.length > 1 ? xs.pop() : null;
}
If you really want just a regular expression, you can use /::((?:[^:]|:(?!:))*)$/. First, it matches a literal ::. Then, we use parentheses to put the desired thing in capturing group 1. The desired thing is one or more copies of a (?:...)-bracketed string; this bracketing groups without capturing. We then look for either [^:], a non-colon character, or :(?!:), a colon followed by a non-colon. The (?!...) is a negative lookahead, which matches only if the next token doesn't match the contained pattern. Since JavaScript doesn't support negative lookbehinds, I can't see a good way to avoid capturing the :: as well, but you can wrap this in a function:
function lastTokenRegex(str) {
var m = str.match(/::((?:[^:]|:(?!:))*)$/);
return m && m[1];
}
var string2 = string.replace(/.*::/, "");
though perhaps string isn't the best choice of name for your string?