V
V
Vasia19932020-01-24 16:45:05
JavaScript
Vasia1993, 2020-01-24 16:45:05

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

2 answer(s)
S
Sergey Pankov, 2020-01-24
@Vasia1993

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}`);
    });
}

L
ljutaev, 2020-01-24
@ljutaev

$('input[name="phone"]').on('change keyup', function() {
    var val = $(this).val();
    var digit = ('' + val)[2];
    if (digit == '0') {
      $(this).val(' ');
    }
  });

so I check that the third character 0 is not for the phone, we can help with something

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question