RegExp: match known number - javascript

i want to match a known number(floating point or integer) with a string and finding number of that repeat.
like match 22 in "22.5 98 12 22 322"
how i can do it with regular expression ?

try using this:
if (numbers.indexOf("22") != -1) {
//The number 22 is in the String
}
It's not exactly a regex but why use it when you don't need it?

str.match(/substr/g).length
returns number of occurrences of "substr" in "str". For example:
str = "22.5 98 12 22 322"
num = str.match(/22/g).length
alert(num) // 3
If you want to match integers rather than just substrings, make sure the number is preceded and followed by a space:
str = "22.5 98 12 22 322"
num = str.match(/(^|\s)22($|\s)/g).length
alert(num) // 1
Another option, without regular expressions:
str = "22.5 98 12 22 322"
num = str.split(" ").filter(function(item) {
return Number(item) === 22;
}).length;

If you mean you should find only the numbers that repeat within the same string, you can use something like this:
var test_str = '22 34 55 45345 34 22 555';
var matches = test_str.match(/(\b[0-9]+\b)(?=.+\b\1\b)/g);
// matches contains ["22", "34"], but not "55"

var str = "22.5 98 12 22 322";
var pattern = /22/g;
if (pattern.test(str)) {
// it matches "22", do your logic here...
}
see RegExp and RegExp.test(string) for more

Corrolating integer/floating point numbers from text to regex is
very tricky, since regex has no such concept.
With dynamic input, all the work is constructing the regex programatically.
This example doesn't include scientific notation, but it could.
Pseudo code:
If the input matches this parse regex
/^ (?=.*\d) // must be a digit in the input
([+-]?) // capt (1), optional +/-
[0]* // suck up all leading 0's
(\d*) // capt (2), optional digits, will start with [1-9]
([.]?) // capt (3), optional period
(\d*?) // capt (4), non-greedy optional digits, will start with [1-9]
[0]* // suck up all trailing 0's
$/x
sign = '[+]?';
If !length $2 AND !length $4
sign = '[+-]';
Else
If $1 == '-'
sign = '[-]';
Endif
Endif
whole = '[0]*';
If length $2
whole = '[0]*' + $2;
Endif
decfrac = '[.]? [0]*';
If length $4
$decfrac = '[.] ' + $4 + '[0]*';
Endif
regex = '/(?:^|\s)(?=\S*\d) ' + "$sign $whole $decfrac" + ' (?:\s|$)/x';
Else
// input is not valid
Endif
This is a Perl test case.
The dynamic regex produced has not been tested,
I asume it works but I could be wrong.
#samps = (
'00000.0',
'+.0',
'-.0',
'0.',
'-0.00100',
'1',
'+.1',
'+43.0910',
'22',
'33.e19',
);
for (#samps)
{
print "\nInput: $_\n";
if( /^ (?=.*\d) ([+-]?) [0]*(\d*) ([.]?) (\d*?)[0]*$/x ) # 1,2,3,4
{
my ($sign, $whole, $decfrac);
$sign = '[+]?';
if ( !length $2 && !length $4) {
$sign = '[+-]';
} elsif ($1 eq '-') {
$sign = '[-]';
}
$whole = '[0]*';
if ( length $2) {
$whole = '[0]*'.$2;
}
$decfrac = '[.]? [0]*';
if ( length $4) {
$decfrac = '[.] '.$4.'[0]*';
}
my $regex = '/(?:^|\s)(?=\S*\d) '. "$sign $whole $decfrac" . ' (?:\s|$)/x';
print "Regex: $regex\n";
}
else {
print "**input '$_' is not valid\n";
next;
}
}
Output
Input: 00000.0
Regex: /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x
Input: +.0
Regex: /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x
Input: -.0
Regex: /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x
Input: 0.
Regex: /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x
Input: -0.00100
Regex: /(?:^|\s)(?=\S*\d) [-] [0]* [.] 001[0]* (?:\s|$)/x
Input: 1
Regex: /(?:^|\s)(?=\S*\d) [+]? [0]*1 [.]? [0]* (?:\s|$)/x
Input: +.1
Regex: /(?:^|\s)(?=\S*\d) [+]? [0]* [.] 1[0]* (?:\s|$)/x
Input: +43.0910
Regex: /(?:^|\s)(?=\S*\d) [+]? [0]*43 [.] 091[0]* (?:\s|$)/x
Input: 22
Regex: /(?:^|\s)(?=\S*\d) [+]? [0]*22 [.]? [0]* (?:\s|$)/x
Input: 33.e19
**input '33.e19' is not valid

Here's a good tool for you to explore regular expressions :)
http://www.regular-expressions.info/javascriptexample.html
Adopted from the site above:
function demoShowMatchClick() {
var re = new RegExp("(?:^| )+(22(?:$| ))+");
var m = re.exec("22.5 98 12 22 322");
if (m == null) {
alert("No match");
} else {
var s = "Match at position " + m.index + ":\n";
for (i = 0; i < m.length; i++) {
s = s + m[i] + "\n";
}
alert(s);
}
}
This will give you all matches and requires the number to be seperated by beginning or end of string or a whitespace.

Related

remove space from string between all numbers and percentage (javascript regex)

I have to remove all spaces between number and percentage sign. String can contain all other symbols as well. Space should be removed only between number and percentage not any other symbol and percentage.
Examples:
str = "test 12 %"; // expected "test 12%"
str = "test 1.2 %"; // expected "test 1.2%"
str = "test 1,2 % 2.5 %"; // expected "test 1,2% 2.5%"
str = " % test 1,2 % 2.5 % something"; // expected " % test 1,2% 2.5% something"
I think that I have managed to create regexp that matches "decimal number with percentage sign", however, I do not know how to do "replace space in this matching number".
var r = /\(?\d+(?:\.\d+)? ?%\)? */g;
Code
See regex in use here
(\d) +(?=%)
Replacement: $1
Usage
const regex = /(\d) +(?=%)/g
const a = ['test 12 %','test 1.2 %','test 1,2 % 2.5 %',' % test 1,2 % 2.5 % something']
const subst = `$1`
a.forEach(function(str) {
console.log(str.replace(regex, subst))
})
Explanation
(\d) Capture a digit into capture group 1
+ Match one or more spaces
(?=%) Positive lookahead ensuring what follows is a %

match string after the last colon

i have the following string
foo: 21, bar: 11
where foo and bar are not constants, so I'm trying to match all the digits after the last ":"(colon) character.
const myString = 'foo: 21, bar: 11'
myString.match(/\d+/g).shift().join() -> 11
can i do the same just with pure regex?
thanks!
Using negative regex you can use this regex:
/\d+(?![^:]*:)/g
RegEx Demo
(?![^:]*:) is negative lookahead to assert that there is no : ahead of digits we are matching.
Code Demo:
var myString = 'foo: 21, bar: 11';
console.log(myString.match(/\d+(?![^:]*:)/g));
var myString = 'foo: 21, bar: 11';
console.log(myString.replace(/(.)*\:\s?/,''));
You may use either of the two solutions:
This one will match up to the last : and grab the digits after 0+ whitespaces:
var s = "foo: 21, bar: 11";
var m = s.match(/.*:\s*(\d+)/);
if (m !== null) {
console.log(m[1]);
}
And this one will find a : followed with 0+ whitespaces, then will capture 1+ digits into Group 1 and then will match 0+ chars other than : up to the end of string.
var s = "foo: 21, bar: 11";
m1 = s.match(/:\s*(\d+)[^:]*$/);
if (m1 !== null) {
console.log(m1[1]);
}

What should be the regex to match the start of a string

I have a string something like and I want to match just the first '{' of every {{xxxx}} pattern
{{abcd}}{{efg}}{{hij}}
{{abcd}}{{efg}}{{hij}}{
I tried with /(\s|^|.){/g but this pattern matches
{{abcd}}{{efg}}{{hij}}
Can some one guide me in the right direction
You need to use /(^|[^{]){/g (that matches and captures into Group 1 start-of-string or any char other than {, and then matches a {) and check if Group 1 matched at each RegExp#exec iteration. Then, if Group 1 matched, increment the match index:
var re = /(^|[^{]){/g;
var str = "{{abcd}}{{efg}}{{hij}}\n{{abcd}}{{efg}}{{hij}}{";
// 0 8 15 23 31 38 45
var m, indices = [];
while ((m = re.exec(str)) !== null) {
indices.push(m.index + (m[1] ? 1 : 0));
}
console.log(indices);

Regular Expressions first Alphabetic rest alpanumeric

I'm trying to write a Regex
What I need is:
To start only with: A-z (Alphabetic)
Min. Length: 5
Max. Length: 10
The rest can be A-z0-9(Alphanumeric) but contain at least one number
What I have: ^[A-z][A-z0-9]{5,10}$
You can use
/^(?=.{5,10}$)[a-z][a-z]*\d[a-z\d]*$/i
See the regex demo
Details:
^ - start of string
(?=.{5,10}$) - the string should contain 5 to 10 any chars other than line break chars (this will be restricted by the consuming pattern later) up to the end of string
[a-z] - the first char must be an ASCII letter (i modifier makes the pattern case insensitive)
[a-z]* - 0+ ASCII letters
\d - 1 digit
[a-z\d]* - 0+ ASCII letters of digits
$ - end of string.
var ss = [ "ABCABCABC1","ABCA1BCAB","A1BCABCA","A1BCAB","A1BCA","A1BC","1BCABCABC1","ABCABC","ABCABCABCD"]; // Test strings
var rx = /^(?=.{5,10}$)[a-z][a-z]*\d[a-z\d]*$/i; // Build the regex dynamically
document.body.innerHTML += "Pattern: <b>" + rx.source
+ "</b><br/>"; // Display resulting pattern
for (var s = 0; s < ss.length; s++) { // Demo
document.body.innerHTML += "Testing \"<i>" + ss[s] + "</i>\"... ";
document.body.innerHTML += "Matched: <b>" + rx.test(ss[s]) + "</b><br/>";
}
var pattern = /^[a-z]{1}\w{4,9}$/i;
/* PATTERN
^ : Start of line
[a-z]{1} : One symbol between a and z
\w{4,9} : 4 to 9 symbols of any alphanumeric type
$ : End of line
/i : Case-insensitive
*/
var tests = [
"1abcdefghijklmn", //false
"abcdefghijklmndfvdfvfdv", //false
"1abcde", //false
"abcd1", //true
];
for (var i = 0; i < tests.length; i++) {
console.log(
tests[i],
pattern.test(tests[i])
)
}

Regex for hexa decimal combination of string

I need a regex which will test the combination of hexadecimal string. Specifically, the string must have:
Atleast one digit in the range 0 - 9,
At least one digit in the range A - F,
be 12 characters long.
String should not start with 0
Currently I am using var macRegex = new RegExp("^[0-9A-Fa-f]{12}$");. The above regex allow strings like "111111111111" , "000000000000" which i want to avoid . It should allow a string like "944a0c122123"
How can I accomplish this?
To keep the regular expression simple, I'd separate matching the pattern and checking the length:
var re = /^(\d+[a-f]+[\da-f]*|[a-f]+\d+[\da-f]*)$/i;
var s = '011001aFFA77';
console.log(re.test(s) && s.length == 12); // true
var s = '000000000000';
console.log(re.test(s) && s.length == 12); // false
The pattern to match:
one or more digits followed by one or more letters a to f followed by zero or more digits or letters a to f, OR
one or more letters a to f followed by one or more digits followed by zero or more digits or letters a to f
then check the length is 12.
Edit
To meet the new criterion "can't start with 0" (and simplify the expression a bit), the regular expression can be:
var re = /^([1-9]\d*[a-f]+|[a-f]+\d+)[\da-f]*$/i;
var s = '011001aFFA77';
console.log(re.test(s) && s.length == 12); // false
var s = '000000000000';
console.log(re.test(s) && s.length == 12); // false
var s = '011001aFFA77';
console.log(re.test(s) && s.length == 12); // false
var s = 'a11001aFFA77';
console.log(re.test(s) && s.length == 12); // true
var s = '311001aFFA77';
console.log(re.test(s) && s.length == 12); // true
var s = '0000000000a1';
console.log(re.test(s) && s.length == 12); // false
If you don't mind a non RegExp solution this function should do what you want.
function only_hex_len_12(str){
// if the string is the wrong length return false
if(str.length!=12) return false;
var hex_val=parseInt(str,16);
var dec_val=parseInt(str,10);
// If the string is dec return false
if(!isNaN(dec_val)) return false;
// If the string is not hex return false
if(isNaN(hex_val)) return false;
// Otherwise the string is okay so return true
return true;
}
If you want a RegExp solution RobG's answer looks good.
Here is the way I'd go:
/^(?=.*\d)(?=.*[a-f])[1-9a-f][\da-f]{10}[1-9a-f]$/i
Explanation:
/
^ : begining of string
(?=.*\d) : lookahead, at least one digit
(?=.*[a-f]) : lookahead, at least one letter in range a-f
[1-9a-f] : first character, not zero
[\da-f]{10} : ten hex character
[1-9a-f] : last character, not zero
$ : end of string
/i : case insensitive
You can use a set of positive lookaheads to do this.
Here's the regex:
/(?!0).(?=.*\d)(?=.*[a-f])[\da-f]{11}/i
(?!0) Negative lookahead
. Match any character
(?= Positive lookahead
.* Match any character zero to unlimited
\d Match a digit
)
(?= Positive lookahead
.* Match any character zero to unlimited
[a-f] Match a character a-f
)
[\da-f]{11} Match any of the valid characters, 12 times
You can conceptualize this is a logical AND of the expressions in each lookahead (except for the .*).
var regexp = /(?!0).(?=.*\d)(?=.*[a-f])[\da-f]{11}/i;
var test1 = "000000000000";
var test2 = "111111111111";
var test3 = "8179acf0871a";
var test4 = "9abzzzzzzzzz";
var test5 = "0179acf0871a";
console.log(regexp.test(test1));
console.log(regexp.test(test2));
console.log(regexp.test(test3));
console.log(regexp.test(test4));
console.log(regexp.test(test5));
Here's a snippet that demonstrates similar input samples.

Categories