0

Say you have to parse strings to match something like this :

913|723

being xxx|yyy the coordinates.

How do I check with javascript and regex if the string matches the xxx|yyy (only numbers and the separator) ?

2 Answers 2

3
if (/^\d+\|\d+$/.test(subject)) {
    // Successful match
} 

Explanation:

^   # Start of string
\d+ # One or more digits
\|  # literal |
\d+ # One or more digits
$   # End of string
Sign up to request clarification or add additional context in comments.

Comments

2

You can use regex pattern

^\d{3}\|\d{3}$

var pattern = '/^\d{3}\|\d{3}$/';
if (pattern.test(subject)) {
    alert("matched");
} 

You can online test here

2 Comments

This is not correct. It will report a match on "abc123|234def".
You can try it above regex online test suite, and you will see that "abc1|2def" wont match

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.