简体   繁体   中英

How to Get Specific text from string address using regular expression?

I have been trying to extract the 'XYZ Name' from this address:

*Address: *

XYZ Name
X Address
Y Address
Z Address

if we have an address like this:

Address:
XYZ Name
X Address
Y Address
Z Address

then we can use the following regular expression to get Address and XYZ Name:

var myRegex1 = /Address\s*.*/mi;
var bracket2 = myRegex2.exec(msgBody);

But due to the changed format of Address to *Address: *, this regular expression doesn't work anymore. Any suggestion would be much appreciated.

So from the regular expression provided by @The Fourth bird, I was able to extract the required text mentioned in the question. So following snippet can be used for that:

var myRegex2 = / *\*?Address\s*\*?\s*(.+)/g;
var bracket2 = myRegex2.exec(msgBody); 
Logger.log(bracket2[bracket2.length-1]);

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