简体   繁体   中英

Regex match alert message

These may appear multiple times in a large string. Javascript Regex..

Trying to match both these cases - (the bolded text): First, ends with ), second ends with the 1st colon.

sometext sometext Alert Message: \\\\SERVERNAME: TFTP service is down - no heartbeat. (\\\\SERVERNAME)

and this:

IP Address: 1.1.1.1 : Alert Message: device name is unreachable : Event Class: /Status/Ping : Site:

This works for 2nd , but not for 1st

.match(/Alert Message:.*? :/g)

What rx will match both cases?

You can use a pipe '|' to indicate an 'or' in a regular expression. Also you can use .*? to skim over unimportant parts of the text to look for sections that are important. Here is an example of both in effect: http://jsfiddle.net/985Kg/

var regex = new RegExp('(Alert Message.*?TFTP service is down - no heartbeat.|Alert Message: device name is unreachable)');

alert(regex.test('Alert Message: \\SERVERNAME: TFTP service is down - no heartbeat. (\\SERVERNAME)')); // alerts 'true'.

Try the following:

.match(/Alert Message:/g)

This is a lot more general, it might trigger false alarms.

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