简体   繁体   中英

Regular expression to check a string starts with

I am trying to match a string that starts with K, P, J and NI. I am using following regular expression:

^[kpjni](.*)$

However, it does not work as I want. It accepts any character that starts with N or I. How could I force it to match NI not N and I separately.

The regex you're after is this:

/^([kpj]|ni).*$/i

This will match K, P, J, NI, k, p ,j, ni, Ni, and nI at the start of strings, followed by any other combination of characters.

Here's a JSfiddle demonstrating the matching: http://jsfiddle.net/45UU7/

Also, a live test where you can change the value to check what it matches: http://regex101.com/r/fJ2wF3/

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