javascript - 正则表达式 如何根据某个条件是否成立,来选择匹配 2 个可选表达式中的其中一个。
场景:
原先有一个字符串的内容匹配,现在这个字符串改成了另一种样子。但是对于已经发版的客户端还要支持旧版的字符串匹配。
例:
旧字符串: android/360 1.0
新字符串: android/aaa/360/1.0
现在想写一个正则表达式,既想能匹配旧字符串,也想匹配新字符串。
有没有办法根据某个条件成立,来选择其中一种表达式?
旧表达式匹配正则:
([iOS|android])\\scoco/([\\S\\s])\\s([\\S]*)
旧正则测试字符串
android coco/360 1.0
新表达式匹配正则:
"([iOS|android])[\\s|\\S]coco/([\\S])/([\\S])/([\\S])/([0-9])";
新正则测试字符串:
android coco/android_deviceId/360/1.0/123456
如何让一个正则,既可以支持新的,也可以支持旧的?
<code>([iOS|android]*)[\\s|\\S]coco/[([\\S\\s]*)\\s([\\S]*)] `**|**` [([\\S]*)/([\\S]*)/([\\S]*)/([0-9])] </code>
类似于上面这个错误的正则思路。 该如何写?
回复内容:
场景:
原先有一个字符串的内容匹配,现在这个字符串改成了另一种样子。但是对于已经发版的客户端还要支持旧版的字符串匹配。
例:
旧字符串: android/360 1.0
新字符串: android/aaa/360/1.0
现在想写一个正则表达式,既想能匹配旧字符串,也想匹配新字符串。
有没有办法根据某个条件成立,来选择其中一种表达式?
旧表达式匹配正则:
([iOS|android])\\scoco/([\\S\\s])\\s([\\S]*)
旧正则测试字符串
android coco/360 1.0
新表达式匹配正则:
"([iOS|android])[\\s|\\S]coco/([\\S])/([\\S])/([\\S])/([0-9])";
新正则测试字符串:
android coco/android_deviceId/360/1.0/123456
如何让一个正则,既可以支持新的,也可以支持旧的?
<code>([iOS|android]*)[\\s|\\S]coco/[([\\S\\s]*)\\s([\\S]*)] `**|**` [([\\S]*)/([\\S]*)/([\\S]*)/([0-9])] </code>
类似于上面这个错误的正则思路。 该如何写?
你这样两个字符串差别已经很大了,就没有必要压缩到一个正则里了,直接分成两次正则判断更为妥当。
正则里没有根据条件来选择匹配规则的定义,像你这种情况用的还是 |
进行或匹配而已。其效果和使用两次正则没什么区别。
最暴力的解决方案:用“或”(|
)
优雅一些的方案:提取两个要匹配字符串的公有特点,写新的正则表达式兼容

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

The built-in quantization tools on the exchange include: 1. Binance: Provides Binance Futures quantitative module, low handling fees, and supports AI-assisted transactions. 2. OKX (Ouyi): Supports multi-account management and intelligent order routing, and provides institutional-level risk control. The independent quantitative strategy platforms include: 3. 3Commas: drag-and-drop strategy generator, suitable for multi-platform hedging arbitrage. 4. Quadency: Professional-level algorithm strategy library, supporting customized risk thresholds. 5. Pionex: Built-in 16 preset strategy, low transaction fee. Vertical domain tools include: 6. Cryptohopper: cloud-based quantitative platform, supporting 150 technical indicators. 7. Bitsgap:

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.
