Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Regular expression to find 2 digits in a string

Community Expert ,
May 13, 2023 May 13, 2023

Hi,

I am looking for a regexp to for checking a string is containing both the #1 and #3 digits but numbers including these digits are not take in account (12, 368, 51, 6983, etc.);

I couldn't write the expression all at once, so I compiled the search for the digit 1 and the search for the digit 3.

Here is a quick explanation, for finding the digit 1, the regular expression I wrote is:

/(.*[^0-9]1[^0-9].*)/

But that doesn’t work if “1” is at the beginning or at the end of the string, so for testing I add a “x” character at these both places.

And the same for the digit “3”.

Attached is an example of the complete script I wrote.

if (event.value=="") this.getField("light").fillColor=color.transparent;
else if (/(.*[^0-9]1[^0-9].*)/.test("x"+event.value+"x") && /(.*[^0-9]3[^0-9].*)/.test("x"+event.value+"x")) this.getField("light").fillColor=color.green;
else this.getField("light").fillColor=color.red;

I wonder if it's possible to write a single expression for that without adding both characters for testing.

Thanks in advance if somebody has an idea...

@+

TOPICS
JavaScript , PDF forms
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
May 13, 2023 May 13, 2023

Try this:

/(?=.*(^|\D)1(\D|$))(?=.*(^|\D)3(\D|$))/

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 13, 2023 May 13, 2023

Try this:

/(?=.*(^|\D)1(\D|$))(?=.*(^|\D)3(\D|$))/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 13, 2023 May 13, 2023
LATEST

Great, thanks!

That seems so obvious when we have the solution in front of the eyes...

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines