1
1
1thater2020-07-09 20:09:01
JavaScript
1thater, 2020-07-09 20:09:01

How to compose a regular expression with only one + at the beginning, and then only numbers?

form.addEventListener('input', evt => {
  const target = evt.target;
  if (target.name === 'user_phone') {
    target.value = target.value.replace(/123/, '');
  }
});


We need a regular expression that leaves only the first "+" and subsequent digits in the replace method (and there may not be a +).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2020-07-09
@1thater

.replace(/^[^+\d]*(\+|\d)|\D/g, '$1');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question