简体   繁体   中英

How to compare two Strings and get Different part

now I have two strings,

var str1 = "A10B1C101D11";
var str2 = "A1B22C101D110E1";

What I intend to do is to tell the difference between them, the result will look like

A10B1C101D11

A10 B22 C101 D110E1

It follows the same pattern, one character and a number. And if the character doesn't exist or the number is different between them, I will say they are different, and highlight the different part. Can regular expression do it or any other good solution? thanks in advance!

Let me start by stating that regexp might not be the best tool for this. As the strings have a simple format that you are aware of it will be faster and safer to parse the strings into tokens and then compare the tokens.

However you can do this with Regexp, although in javascript you are hampered by the lack of lookbehind.

The way to do this is to use negative lookahead to prevent matches that are included in the other string. However since javascript does not support lookbehind you might need to go search from both directions.

We do this by concatenating the strings, with a delimiter that we can test for.

If using '|' as a delimiter the regexp becomes;

/(\D\d*)(?=(?:\||\D.*\|))(?!.*\|(.*\d)?\1(\D|$))/g

To find the tokens in the second string that are not present in the first you do;

var bothstring=str2.concat("|",str1);
var re=/(\D\d*)(?=(?:\||\D.*\|))(?!.*\|(.*\d)?\1(\D|$))/g;
var match=re.exec(bothstring);

Subsequent calls to re.exec will return later matches. So you can iterate over them as in the following example;

while (match!=null){
    alert("\""+match+"\" At position "+match.index);
    match=re.exec(t);
}

As stated this gives tokens in str2 that are different in str1. To get the tokens in str1 that are different use the same code but change the order of str1 and str2 when you concatenate the strings.

The above code might not be safe if dealing with potentially dirty input. In particular it might misbehave if feed a string like "A100|A100", the first A100 will not be considered as having a missing object because the regexp is not aware that the source is supposed to be two different strings. If this is a potential issue then search for occurences of the delimiting character.

You call break the string into an array

var aStr1 = str1.split('');
var aStr2 = str2.split('');

Then check which one has more characters, and save the smaller number

var totalCharacters;
if(aStr1.length > aStr2.length) {
    totalCharacters = aStr2.length
} else {
    totalCharacters = aStr1.length
}

And loop comparing both

var diff = [];
for(var i = 0; i<totalCharacters; i++) {
   if(aStr1[i] != aStr2[i]) {
      diff.push(aStr1[i]); // or something else
   }
}

At the very end you can concat those last characters from the bigger String (since they obviously are different from the other one).

Does it helps you?

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