Reputation: 107
Have such code: https://github.com/dezostus/googleapp/blob/master/z_draft/email%20parce(test).gs
Problem with this regex it this part:
.match(/Phone:^((8|\+)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$/);
.match(/Comment:\s*([\s\S]+)(?=Email|Phone)/);
Phone dont return anything.. true many things, but finally nothing works. Possible mask is:
12361234567
8029123456
+12361234567
+375 29 123 45 67
4(123)123-45-67
123-45-67
9261234567
Comment works well with:
Comment: ТestABC1234567 Ads
Phone: +313239084
or
Comment: ТestABC1234567 Ads
Email: [email protected]
but when template like this:
Comment: ТestABC1234567 Ads
Phone: +313239084
Email: [email protected]
it return:
ТestABC1234567 Ads
Phone: +313239084
Would be grateful for any variants how to solve this
Upvotes: 2
Views: 513
Reputation: 5564
Since Wiktor has already tackled your first issue, here's a possible solution for your second issue:
/Comment:\s*([\s\S]+?)(?=(?:Phone|Email))/
However, if the words Email or Phone appear in your Comment, you will need to use the following instead, assuming that Phone
and Email
entries always start on newlines:
/Comment:\s*([\s\S]+?)(?=(?:\nPhone|\nEmail))/
var strs = [
'Comment: Comment Type 1 ТestABC1234567 Ads\n\
Email: [email protected]\n\
Phone: +313239084',
'Comment: Comment Type 2 ТestABC1234567 Ads\n\
Phone: +313239084\n\
Email: [email protected]',
'Comment: Comment Type 3 ТestABC1234567 Ads\n\
Phone: +313239084',
'Comment: Comment Type 4 ТestABC1234567 Ads\n\
Email: [email protected]'
];
var re = /Comment:\s*([\s\S]+?)(?=(?:Phone|Email))/;
strs.forEach(function(str) {
document.body.insertAdjacentHTML('beforeend', str.match(re).slice(1) + '<br>');
});
Upvotes: 2
Reputation: 627469
I'd rather use
/Phone:\s*((?:[48+][- ]?)?(?:\(?\d{3}\)?[- ]?)?[\d -]{7,10})/
See the regex demo
I added 4
as an alternative to what may be at the beginning (one of the numbers starts with 4
). So, I used [48+]
(either 4
or 8
or +
).
I removed unnecessary escape symbols, no need to escape the hyphen in the initial or final positions in the character class.
I removed the redudant capturing groups or turned into non-capturing, and set 1 capturing group you need that stores the actual number (it will be Group 1).
I also removed the anchors, they can be used only with the /m
modifier since the input is multiline. However, there may be leading trailing spaces. If you really want to keep the ^
and $
, try /^\s*Phone:\s*((?:[48+][- ]?)?(?:\(?\d{3}\)?[- ]?)?[\d -]{7,10})\s*/m
.
Upvotes: 2