简体   繁体   中英

How to match a string contain specific text preceded by any combination of alphanumerics and other charcters in regex

I want to match strings which have a specific text in start but after that any combination of alphanumeric and character value and string ends with double quotes " . Here is the sample string

fixed_words_/abcd123/"

in this string, fixed_words_ will always be same and in the end will be " but in between there can be digits, alphabets, underscores and slashes.

I tried mystring.match(/fixed_words_\\w*"/g) but its not working. I am sorry but I am new to regex so don't mind if its a stupid question.

Instead of \\w , have a character class that can match either \\w (alphanumerics/underscores) or a slash:

mystring.match(/fixed_words_[\/\w]*"/g)

The above assumes that your expression can appear anywhere (or multiple times!) in mystring . If you want mystring to contain only your expression, add a start-of-string anchor ( ^ ) at the beginning, an end-of-string anchor ( $ ) at the end, and get rid of the g flag permitting multiple matches:

mystring.match(/^fixed_words_[\/\w]*"$/)

Use the following regex to match your string: ^\\s*fixed_words_[^"]"\\s*$

[^"]* will match all the characters until it finds a double quote ( " ) character.

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