Javascript regex for string with a number in it - javascript

I'm currently working within an AngularJS directive and in the template I'm attempting to check if a an instance variable of the Controller is a certain type of string.
Specifically, this string can be anything at all so long as it has an 8-digit number in it.
Passing Examples: "gdbfgihfb 88827367 dfgfdg", "12345678", ".12345678"
The number has to be a solid string of 8 numbers with nothing in between.
I've tried this:
$ctrl.var == /[0-9]{8}/
But it doesn't work for some reason. How do I construct a regex in order to do this?
Thanks

Your regex is fine but the comparison is wrong. You want
/\d{8}/.test($ctrl.var)
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
let tests = ["gdbfgihfb 88827367 dfgfdg", "12345678", ".12345678", "nope, no numbers here"],
rx = /\d{8}/;
tests.map(str => document.write(`<pre>"${str}": ${rx.test(str)}</pre>`))

Code:
var first = "gdbfgihfb 88827367 dfgfdg";
var second = "12345678";
var third = ".12345678";
var reg = new RegExp('[0-9]{8}');
console.log(first.match(reg));
console.log(second.match(reg));
console.log(third.match(reg));
Output:
[ '88827367', index: 10, input: 'gdbfgihfb 88827367 dfgfdg' ]
[ '12345678', index: 0, input: '12345678' ]
[ '12345678', index: 1, input: '.12345678' ]

Related

Regex to get every character that appears between equals sign and text

I would like to get all the text/numbers that appear after the equals sign such that this input
"Input: m = 2, n = 3, indices = [[0,1],[1,1]]"
Would return this output:
[2,3, [[0,1],[1,1]] ]
This is what I have tried:
eachEx.match(/= (.+)/)[1]
However this returns:
2, n = 3, indices = [[0,1],[1,1]]
I have thought of splitting the string and iterating through each element, passing it through the match I have. However, the problem is that I would lose the ability to know whether or not the element in question was meant to be a string or an integer or an array. I need to know this information
I won't be surprised if you end up needing to write a simple parser for this, rather than a single regex. But the specific example given can be done with a single regex. It's just that I suspect when you throw more examples at it, it'll become too complicated.
If the thing that makes the , a delimiter after the 2 and 3 but not in the final match is that the final match is wrapped in [___], then you can use an alternation to adjust what characters are allowed in the capture:
/= (\[[^=]+\]|[^=,]+)/
That says that if the text starts with [ and ends with ], match all non-= inside it. Otherwise, match all non-= and non-,.
Then, to get all the matches, add a g flag and use matchAll, then post-process the iterable you get from it to extract the capture groups:
const eachEx = "Input: m = 2, n = 3, indices = [[0,1],[1,1]]";
const match = eachEx.matchAll(/= (\[[^=]+\]|[^=,]+)/g);
console.log(Array.from(match, ([, capture]) => capture));
As an example of a string that would be parsed incorrectly by that, consider "a = [3, [2, ], b = 3", which gives us the array [ "[3, [2, ]", "3" ] when probably it should be an error:
const eachEx = "Input: a = [3, [2, ], b = 3";
const match = eachEx.matchAll(/= (\[[^=]+\]|[^=,]+)/g);
console.log(Array.from(match, ([, capture]) => capture));
Hence the warning above that you may need to write a simple parser instead.

Regex matching of range with pre-defined labels

I have the following regex that allows any number (x), or comma separated numbers (x,x), or any range (x-x) for an input on my page:
^\d+(?:(?:\s*-\s*\d+)?|(?:\s*,\s*\d+)*)$
This works perfectly but I have a new requirement. I need to allow the user to label what they are defining and allow that if it's valid. For example, they can say: Buildings: 1000-1010 or Buildings: 1001,1002.
Buildings: must be in the beginning of the string followed a colon followed by the rest. If they don't specify I won't know what they are saying.
Currently Buildings is the only label they can define, but I know more will be coming so I would also like to be able to specify that, if possible. But hopefully I can figure it out if I figure out how to allow one:
let input = 'Buildings: 1000-1001';
/^\d+(?:(?:\s*-\s*\d+)?|(?:\s*,\s*\d+)*\s*,?)$/.test(input); // should be ok
input = 'Buildings: 1000';
/^\d+(?:(?:\s*-\s*\d+)?|(?:\s*,\s*\d+)*\s*,?)$/.test(input); // should be ok
input = 'Buildings: 1000,2002';
/^\d+(?:(?:\s*-\s*\d+)?|(?:\s*,\s*\d+)*\s*,?)$/.test(input); // should be ok
For future, say I have an array of labels:
const categories: ['Buildings', 'Cars', 'Houses'];
To loop over this list and run the test and include the variable's value in the regex. Is it possible?
Are you supposed to hardcode the text? I started trying that but I kept getting errors.
Thank you
A simpler regex should do the trick along with some format strings.
for example a simpler regex like /\d+?([,-]\d+?)*?$/ should do fine matching all three of your examples.
And for the string substitution using the new RegExp syntax will allow you to use standard format strings.
For Example:
const categories = ['Buildings', 'Cars', 'Houses'];
categories.forEach((i) => {
let input = 'Buildings: 1000-1001';
let re = new RegExp(`${i}: \\d+?([,-]\\d+?)*?$`);
console.log(re.test(input));
input = 'Buildings: 1000';
re = new RegExp(`${i}: \\d+?([,-]\\d+?)*?$`);
console.log(re.test(input));
input = 'Buildings: 1000,2002,2003,2034';
re = new RegExp(`${i}: \\d+?([,-]\\d+?)*?$`);
console.log(re.test(input));
input = 'Buildings: 1000,2002';
re = new RegExp(`${i}: \\d+?([,-]\\d+?)*?$`);
console.log(re.test(input));
});
OUTPUT : Only the first 4 print True because Only the first element of the array is in each of the inputs.
true
true
true
true
false
false
...
If the labels do not contain any special regex meta character, you might shorten the pattern and use an alternation
^(?:Buildings|Cars|Houses):\s*\d+(?:\s*[-,]\s*\d+)*$
See a regex demo.
const categories = ['Buildings', 'Cars', 'Houses'];
const regex = new RegExp(`^(?:${categories.join("|")}):\\s*\\d+(?:\\s*[-,]\\s*\\d+)*$`);
[
"Buildings: 1000-1001",
"Buildings: 1000",
"Buildings: 1000,2002",
"Test: 1000,2002"
].forEach(s => console.log(`${s} --> ${regex.test(s)}`));

RegExp object won't execute more than once, why?

I stored a RegExp object in a variable and used it for mapping an array of strings into an array of objects (parsed e-mail recipients), but it doesn't work, as if a RegExp object couldn't run its .exec() method more than once.
However, if I use a regular expression literal instead of the stored object, it works as intended.
I cannot understand the reason behind this behavior. Is it expected, or could it be a bug?
The code:
const pattern = /^\s*(?<name>\w.*?)?\W+(?<address>[a-zA-Z\d._-]+#[a-zA-Z\d._-]+\.[a-zA-Z\d_-]+)\W*$/gi;
const input = "John Doe jdoe#acme.com; Ronald Roe <rroe#acme.com>";
const splitValues = input.split(/[\r\n,;]+/).map(s => s.trim()).filter(s => !!s);
const matchGroups1 = splitValues.map(s => pattern.exec(s));
console.log('Using pattern RegExp object:', JSON.stringify(matchGroups1, null, 2));
const matchGroups2 = splitValues.map(s => /^\s*(?<name>\w.*?)?\W+(?<address>[a-zA-Z\d._-]+#[a-zA-Z\d._-]+\.[a-zA-Z\d_-]+)\W*$/gi.exec(s));
console.log('Using literal regular expression:', JSON.stringify(matchGroups2, null, 2));
The output:
[LOG]: "Using pattern RegExp object:", "[
[
"John Doe jdoe#acme.com",
"John Doe",
"jdoe#acme.com"
],
null
]"
[LOG]: "Using literal regular expression:", "[
[
"John Doe jdoe#acme.com",
"John Doe",
"jdoe#acme.com"
],
[
"Ronald Roe <rroe#acme.com>",
"Ronald Roe",
"rroe#acme.com"
]
]"
test in TypeScript playground
The difference lies in the /g flag that you've passed to both regexes. From MDN:
RegExp.prototype.exec() method with the g flag returns each match and its position iteratively.
const str = 'fee fi fo fum';
const re = /\w+\s/g;
console.log(re.exec(str)); // ["fee ", index: 0, input: "fee fi fo fum"]
console.log(re.exec(str)); // ["fi ", index: 4, input: "fee fi fo fum"]
console.log(re.exec(str)); // ["fo ", index: 7, input: "fee fi fo fum"]
console.log(re.exec(str)); // null
So /g on a regex turns the regex object itself into a funny sort of mutable state-tracker. When you call exec on a /g regex, you're matching and also setting a parameter on that regex which remembers where it left off for next time. The intention is that if you match against the same string, you won't get the same match twice, allowing you to do mutable tricks with while loops similar to the sort of way you would write a global regex match in Perl.
But since you're matching on two different strings, it causes problems. Let's look at a simplified example.
const re = /a/g;
re.exec("ab"); // Fine, we match against "a"
re.exec("ba"); // We start looking at the second character, so we match the "a" there.
re.exec("ab"); // We start looking at the third character, so we get *no* match.
Whereas in the case where you produce the regex every time, you never see this statefulness, since the regex object is made anew each time.
So the summary is: Don't use /g if you're planning to reuse the regex against multiple strings.
See Why does Javascript's regex.exec() not always return the same value?. The issue is that exec is stateful: in other words it starts the next search after the index of the last one. You can avoid the issue by including pattern.lastIndex = 0; in the map; or else by using a literal as you suggest; or probably better, by removing the global (/g) flag on the regular expression, as per the other answer here.
const pattern = /^\s*(?<name>\w.*?)?\W+(?<address>[a-zA-Z\d._-]+#[a-zA-Z\d._-]+\.[a-zA-Z\d_-]+)\W*$/gi;
const input = "John Doe jdoe#acme.com; Ronald Roe <rroe#acme.com>";
const splitValues = input.split(/[\r\n,;]+/).map(s => s.trim()).filter(s => !!s);
const matchGroups1 = splitValues.map(s => {pattern.lastIndex = 0; return pattern.exec(s)});
console.log('Using pattern RegExp object:', JSON.stringify(matchGroups1, null, 2));
const matchGroups2 = splitValues.map(s => /^\s*(?<name>\w.*?)?\W+(?<address>[a-zA-Z\d._-]+#[a-zA-Z\d._-]+\.[a-zA-Z\d_-]+)\W*$/gi.exec(s));
console.log('Using literal regular expression:', JSON.stringify(matchGroups2, null, 2));
Playground link

How to match particular strings in an array using javascript?

i have an array of strings like below,
const arr = [
"list-domain/1/",
"list-domain/1/list/1",
"some-group/2/",
"some-group/2/list/2",
"list/3/",
"list/3/item/1"
];
as seen from above array i want to check if the array contains strings of kind
"list-domain/1/" or "some-group/2/" or "list/3/"
here the numbers after / can be anything. meaning in "list-domain/1" here 1 can be 2 or 3 anynumber i have to check if the string contains list-domain or some-group or list followed by / and any number followed by /
so for the above array the expected output is true.
now consider array below,
const arr1 = [
"list-domain/1/list/1",
"some-group/2/list/2",
"list/3/item/1"
];
for arr1 the expected output is false althought there is strings list-domain/1, some-group/2 and list/3 but they are followed by someother strings too like list-domain/1/list/1, some-group/2/list/2, list/3/item/1
so i want to find the exact strings numbers can be anything after /. could someone help me how to do this. thanks.
EDIT:
i want it to return true if the string has "list/1/" or
any string followed by "list/1" but return false if "list/1" is followed by someother string like below
"list-domain/1/list/1/item/1"
true in cases below
"list-domain/1/list/1"
"list/2"
"some-group/3/list/3"
This gives you the required output using Regular Expression (RegExp):
arr.some(a=> new RegExp(/^(list-domain|some-group|list)\/[0-9]+\/$/g).test(a))
If you can count on the shape of pattern being always a string identifier followed by a numerical id a parser might look as follows:
const arr = [
"list-domain/1/",
"list-domain/1/list/1",
"some-group/2/",
"some-group/2/list/2",
"list/3/",
"list/3/item/1"
];
const parse = (str) => {
const parts = str.split('/');
return parts.reduce((parsed, part, i, parts) => {
if (part === '') return parsed;
if (i % 2 === 0) {
parsed[part] = null;
} else {
parsed[parts[i-1]] = part;
}
return parsed;
}, {});
}
arr.forEach(s => {
console.log(parse(s));
})
// returns:
// {list-domain: "1"}
// {list-domain: "1", list: "1"}
// {some-group: "2"}
// {some-group: "2", list: "2"}
// {list: "3"}
// {list: "3", item: "1"}
It returns an object where keys are the string identifiers and values numerical ids. However, as I said, it relies on the exact shape of the string and regular alteration of an identifier and a value.
Use a regular expression. Here is an example.
const rx = /^(([^\/]+)\/(\d+)\/)+$/
// or without the groups
const rxNoGroups = /^([^\/]+\/\d+\/)+$/
const found = items.filter(it => rx.test(it))
This regex tests
start of input
not-a-slash
slash
number
slash
end of input or next group
Tailor it to your needs. I coded it so your non-number words can still contain numbers, so foo/2/bar3/4/. You can also limit number of repetitions.
You can also just test that the string ends with /.

lodash not trimming end and start

I got an array containing strings.
I want to make sure there are no space fillings in end or start of string, in middle is ok.
So I've tried using lodash by doing this:
var answers = req.body.answer.split(/;,/); // req.body.answer = 'nio,9'
answers = _.map(answers, _.trimEnd);
answers = _.map(answers, _.trimStart);
The result is this:
[ 'nio , 9' ] // answer before trim
[ 'nio , 9' ] // answer after trim
The wanted result is:
[ 'nio', '9']
I think the problem in your code is your regular expression you are using, you are trying to split the string where there are ;, and I'm guessing you just want to split by , since your result is an array with only one string inside:
[ 'nio , 9' ]
you should use this regular exp instead:
var answers = req.body.answer.split(/,/);
or without any regular exp just do:
var answers = req.body.answer.split(',');

Categories