简体   繁体   中英

How to check if my string contains a hyphen, and if it does, how to fetch the string before that hyphen?

The string should contain only one hyphen, eg.if the string is ABC-123, then I need to fetch ABC

You may match on the following pattern:

^[^-]+

This will match the first part of a hyphenated input. It also will match the entire input should the input not have any hyphen at all.

Here is a regex demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM