How does a javascript-regular expression match one of the two optional expressions based on whether a condition is true.

Source: Internet
Author: User
Scenario: the content of a character string is matched, and the character string is changed to another one. However, the client of the released version must support string matching of the old version. Example: Old string: android3601.0 New String: androidaaa3601.0 now want to write a regular table... scenario:
The content of a character string is matched, and the character string is changed to another one. However, the client of the released version must support string matching of the old version.

Example:
Old string: android/360 1.0
New String: android/aaa/360/1 .0

Now I want to write a regular expression that matches both the old string and the new string.
Is there a way to select an expression based on a condition?

Old expressions match Regular Expressions:
([IOS | android]) \ Scoco/([\ S \ s]) \ S ([\ S] *)
Old regular expression test string
Android coco/360 1.0

The new expression matches the regular expression:
"([IOS | android]) [\ S | \ S] coco/([\ S])/([\ S])/([\ S])/([0-9]) ";
New Regular Expression test string:
Android coco/android_deviceId/360/1 .0/123456

How can we make a regular expression support both new and old expressions?

([iOS|android]*)[\\s|\\S]coco/[([\\S\\s]*)\\s([\\S]*)]  `**|**`  [([\\S]*)/([\\S]*)/([\\S]*)/([0-9])]

Similar to the above error regular expression. How to write?

Reply content:

Scenario:
The content of a character string is matched, and the character string is changed to another one. However, the client of the released version must support string matching of the old version.

Example:
Old string: android/360 1.0
New String: android/aaa/360/1 .0

Now I want to write a regular expression that matches both the old string and the new string.
Is there a way to select an expression based on a condition?

Old expressions match Regular Expressions:
([IOS | android]) \ Scoco/([\ S \ s]) \ S ([\ S] *)
Old regular expression test string
Android coco/360 1.0

The new expression matches the regular expression:
"([IOS | android]) [\ S | \ S] coco/([\ S])/([\ S])/([\ S])/([0-9]) ";
New Regular Expression test string:
Android coco/android_deviceId/360/1 .0/123456

How can we make a regular expression support both new and old expressions?

([iOS|android]*)[\\s|\\S]coco/[([\\S\\s]*)\\s([\\S]*)]  `**|**`  [([\\S]*)/([\\S]*)/([\\S]*)/([0-9])]

Similar to the above error regular expression. How to write?

There is a big difference between the two strings, so there is no need to compress them into a regular expression. It is more appropriate to divide them into two regular expressions.

There is no matching rule definition in the regular expression based on the condition.|. The effect is no different from the two regular expressions.

The most violent solution: Use "or "(|)

Elegant solution: extract two public features that match strings and write new regular expressions for compatibility

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.