简体   繁体   中英

Regex for case insensitive word Javascript

Is there any easy way to extract values from free form text ?

I am trying to extract vales from this:

"The process started from ITEM: ASDE3423423, Item Group: A_GROUP, ITEM Function: ['/A/B/C'], Price Code: average."

I need o/p which are after ':' based on the key (eg ITEM, Item Group)

ITEM = ASDE3423423
Item Group = A_GROUP
ITEM Function = ['/A/B/C']
Price Code = average

I am using :

ITEM:\s*([^,]+)
Item Group:\s*([^,]+)
ITEM Function:\s*([^,]+)
Price Code:\s*([^,]+)

and splitting the string with ':' and getting str[1] for the given key (eg ITEM, Item Group)

Is there any better way to do this ?

Instead of returning the full match and splitting it return the capture group.

 // DATA STRING var str = 'The process started from ITEM: ASDE3423423, Item Group: A_GROUP, ITEM Function: [\\'/A/B/C\\'], Price Code: average.', // REGEX reg = /ITEM:\\s*([^,]+)|Item Group:\\s*([^,]+)|ITEM Function:\\s*([^,]+)|Price Code:\\s*([^,]+)/g; // GET MATCH match = reg.exec(str); // LOOP MATCH while(match != null) { // LOG MATCH console.log(match[0]) // TRY FOR NEXT MATCH match = reg.exec(str); } 

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