0

I am trying to convert following regex:

^(?:\d{12}|\d{9}|(?i:YHZ)?\d{6}|(?i:YHZ)?\d{8})$

into javascript but facing issues for escaping or handling "(?"

I tried verifying on https://regex101.com/#javascript but unable to handle it can anyone please share what needs to fix in the above regex so it can work in javascript

2
  • what does this regex do? Commented Jan 6, 2016 at 9:57
  • it validates for following cases a. yhz+6numeric (not case sensitive for the yhz) b. yhz+8numeric (not case sensitive for the yhz) c. 6numeric d. 8numeric e. 9 numeric f. 12 numeric Commented Jan 6, 2016 at 9:58

2 Answers 2

4

JavaScript doesn't support partial case insensitive patterns. So (?:i is invalid in JS.

Use i flag to make the regex in-case-sensitive

/^(?:\d{12}|\d{9}|(YHZ)?\d{6}|(YHZ)?\d{8})$/i

Regex 101 Demo


From Comments:

it validates for following cases

a.    yhz+6numeric (not case sensitive for the yhz)
b.    yhz+8numeric (not case sensitive for the yhz)
c.    6numeric
d.    8numeric
e.    9numeric
f.    12numeric

The regex can be rewritten as

/^((yhz)?[0-9]{6}([0-9]{2})?|([0-9]{6})(([0-9]{2,3})|([0-9]{6}))?)$/i

Test this regex on Regex101

input:valid {
  color: green;
}
input:invalid {
  color: red;
}
<input type="text" pattern="((yhz|YHZ)?[0-9]{6}([0-9]{2})?|([0-9]{6})(([0-9]{2,3})|([0-9]{6}))?)" />

<br />
<br />it validates for following cases

<ul>
  <li>yhz + 6numeric</li>
  <li>yhz + 8numeric</li>
  <li>6 numeric</li>
  <li>8 numeric</li>
  <li>9 numeric</li>
  <li>12 numeric</li>
</ul>

Regex Visual

If you don't want any capture group, use (?: at the beginning of group.

/^(?:(?:yhz)?[0-9]{6}(?:[0-9]{2})?|(?:[0-9]{6})(?:(?:[0-9]{2,3})|(?:[0-9]{6}))?)$/i

Regex101 Demo

Regex Visualization

Sign up to request clarification or add additional context in comments.

1 Comment

I used the code /^((yhz)?[0-9]{6}([0-9]{2})?|([0-9]{6})(([0-9]{2,3})|([0-9]{6}))?)$/i but i have to put the following into DHC where i have to add this regex to a key "format_validation_regex": "/^((yhz)?[0-9]{6}([0-9]{2})?|([0-9]{6})(([0-9]{2,3})|([0-9]{6}))?)$/i";, but now it gives error message for anything i enter
0

input:valid {
  color: green;
}
input:invalid {
  color: red;
}
<input type="text" pattern="((yhz|YHZ)?[0-9]{6}([0-9]{2})?|([0-9]{6})(([0-9]{2,3})|([0-9]{6}))?)" />

<br />
<br />it validates for following cases

<ul>
  <li>yhz + 6numeric</li>
  <li>yhz + 8numeric</li>
  <li>6 numeric</li>
  <li>8 numeric</li>
  <li>9 numeric</li>
  <li>12 numeric</li>
</ul>

input:valid {
  color: green;
}
input:invalid {
  color: red;
}
<input type="text" pattern="((yhz|YHZ)?[0-9]{6}([0-9]{2})?|([0-9]{6})(([0-9]{2,3})|([0-9]{6}))?)" />

<br />
<br />it validates for following cases

<ul>
  <li>yhz + 6numeric</li>
  <li>yhz + 8numeric</li>
  <li>6 numeric</li>
  <li>8 numeric</li>
  <li>9 numeric</li>
  <li>12 numeric</li>
</ul>

Comments

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.