[javascript] Regular Expression Question

Asked 2 years ago, Updated 2 years ago, 22 views

Hello. The code below is when you enter your cell phone number XXX-XXXX-XXXX, a code that verifies that the number and the number can be connected through -, /, and\.

var re = /(?:\d{3}|\(\d{3}\))([-\/\.])\d{4}\1\d{4}/;  
var OK = re.exec('010-8269-0021');  

var re = /(?:\d{3})([-\/\.])\d{4}\1\d{4}/;  
var OK = re.exec('010-8269-0021');  

var re = /\d{3}([-\/\.])\d{4}\1\d{4}/;  
var OK = re.exec('010-8269-0021');  

In fact, the code above is the example code shown in mdn below. https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/%EC%A0%95%EA%B7%9C%EC%8B%9D#special-negated-character-set

It's this part.

In the following example, the user is expected to enter a phone number. When the user presses the "Check" button, the script validates the number. If the number is valid (matching the character sequence specified by the regular expression), the script indicates a message to thank the user and a message to confirm the number. If the number is not valid, the script notifies the user that the phone number is not valid.

Non-capturing parentheses (?:), the regular expression finds a three-digit numbers \d{3} OR | In left parentheses (three digits followed by \d{3}, closed parentheses followed by non-capturing parentheses), remember a dash, slash, or decimal point followed by d{3}, a dash of memory match, slash, or a decimal point followed by \1, four digits ([-\/]).

When the user presses the key, the activation change event sets the value of RegExp.input.

<!DOCTYPE html>
<html>  
  <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
    <meta http-equiv="Content-Script-Type" content="text/javascript">  
    <script type="text/javascript">  
      var re = /(?:\d{3}|\(\d{3}\))([-\/\.])\d{3}\1\d{4}/;  
      function testInfo(phoneInput){  
        var OK = re.exec(phoneInput.value);  
        if (!OK)  
          window.alert(OK.input + " isn't a phone number with area code!");  
        else
          window.alert("Thanks, your phone number is " + OK[0]);  
      }  
    </script>  
  </head>  
  <body>  
    <p>Enter your phone number (with area code) and then click "Check".
        <br>The expected format is like ###-###-####.</p>
    <form action="#">  
      <input id="phone"><button onclick="testInfo(document.getElementById('phone'));">Check</button>
    </form>  
  </body>  
</html>

Question1): What is the difference between (?:\d{3}|\(\d{3}\) and (?:\d{3})? I have no idea.

Question2)Do you need (?:\d{3}) in the example above? Can't you just write \d{3}?

The reason for using (?:exp) is because If /foo{1,2}/, {1,2} applies only to the last 'o' of 'foo', so I know that it is used when you want to use it in parentheses, such as /(?:foo){1,2}/.

I don't know if it's necessary to use (?:) in the above question.

Thank you for reading my question! Have a nice day.

regexp javascript

2022-09-22 19:01

1 Answers

Answer to Question 1

If you write \what, look for the literal what. () is a regex special symbol, so if you want to "find" it, you have to escape it with \.

Answer to Question 2
The regex in the example probably wanted to capture this through the /(?:\d{3}|\(\d{3}\))/ syntax.

But you can't capture this at the same time.

The final syntax configured to achieve this is /(?:\d{3}|\(\d{3}\))/. This phrase is composed like this.

I don't know if that's the answer. Use places like https://regex101.com to test cases yourself.


2022-09-22 19:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.