Regex to find all matches for display in a table
Would like to get regex working to return all matches for a given email (or phone) from a larger string.
The string to be searched is structured in space separated rows where each row is like this: U1533|5552223333|fakemail@gmail.com|firstlastname
If there are 4000 rows and a given email matches 7 rows I'd like the matches array to contain all 7 elements, and each match needs to preserve the entire row.
Start of Example code (not working)
var dupltable = "U1533|5122330121,5127624861|bababngaustin@gmail.com|ConradBering U1534|3655483644|DTMNODDTES@GMAIL.COM|JackTodd U1535|5124440936|J.GAMMOWAY09@GMAIL.COM|JohnGammoway U1536|5128045529|busmogga@hotmail.com|KevinBarr "
var temp = "";
var matches = [];
var match = "";
var email = "DTMNODDTES@GMAIL.COM";
var re = new RegExp("(U\d+\|\d+\|"+email+")", "gi");
while (match = re.exec(dupltable)) {
matches.push((match[1]))
}
for (i=0;i<matches.length;i++) {
temp += matches[i]+ " "
}
"found " + matches.length + " matches " + temp
(end of code)
The result of this is a string: found 4 matches d D DD dd
Using Regex101.com tells me the regex pattern is reasonable, but my example seems to match every row and returns gibberish.
The overall purpose is to alert a user after an item exists that it has a duplicate email (or phone or name) to existing contacts.
-
Hi Rich,
cause your source string (dupltable) is structered very well you don't need regex, match, while. A simple nested for-loop does the job.var dupltable = "U1533|5122330121,5127624861|bababngaustin@gmail.com|ConradBering U1534|3655483644|DTMNODDTES@GMAIL.COM|JackTodd U1535|5124440936|J.GAMMOWAY09@GMAIL.COM|JohnGammoway U1536|5128045529|DTMNODDTES@GMAIL.COM|KevinBarr "
var dupltable = dupltable.trim().replace(/ /g," ").split(" ");
var searchFor = "5127624861";
var matches = [];
for(var i = 0; i < dupltable.length; i++){
var subSplit = dupltable[i].split("|");
for(var j = 0; j < subSplit.length; j++){
if(searchFor == subSplit[j] || subSplit[j].indexOf(searchFor +",") > -1 || subSplit[j].indexOf("," + searchFor) > -1){
matches.push(dupltable[i])
}}};
"found " + matches.length + " matches: \n" + matches.join("\n")Result is:
found 1 matches:
U1533|5122330121,5127624861|bababngaustin@gmail.com|ConradBering
Search for DTMNODDTES@GMAIL.COM and you'll see it finds 2 dups (cause I've added that email to another row).Several month ago I've created a duplicate check system based on calculation fields for a contact app with > 15,000 items. Works great.
It returns the result as URLs (links to the found dups) and tells if the phone is a duplicate or the email or both.
On top it creates items in a Duplicate-Handling app where a user can merge the dups. Have implemented that solution now for multiple clients.
Rainer
Please sign in to leave a comment.
Comments
2 comments