Answer the question
In order to leave comments, you need to log in
How to check the regular value of the input that will come after the bracket?
Please tell me how to check the regular expression for the value that came from the input, namely the first number that will come after the opening brackets (?
that is, a number of this type should come:
8 (354), here's how to get to the number 3 with the regular expression and indicate that it should go exactly after the parenthesis
Answer the question
In order to leave comments, you need to log in
There is a great tool for debugging regexps: https://regex101.com/
Here is an example of a regular expression that solves your problem:
The first group will give you your number.
More details: here all characters are greedily skipped up to the bracket, after which there is at least one digit. All numbers after this bracket are collected in group 1.
The same site allows you to generate example code in several languages:
const regex = /.*\((\d+)/gm;
const str = `8(354)1234
8(354 456)789
`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question