1

I have two strings say example

str1 = "The first two have explicit values, but";
str2 = "first two have explicit values, but disabled is empty";

I need to compare the two strings and take out part "first two have explicit values, but"

I tried using 'match' but it returns me null value.

Is there any way to accomplish this with javascript or jQuery?

pravid
  • 719
  • 9
  • 27
  • add what you have tried in OP – guradio Mar 16 '17 at 10:51
  • 2
    *I tried using 'match' but it returns me null value* - please share this code with us to clarify your issue. Also, see [Find the longest common starting substring in a set of strings](http://stackoverflow.com/questions/1916218/find-the-longest-common-starting-substring-in-a-set-of-strings). – Wiktor Stribiżew Mar 16 '17 at 10:53
  • Is the purpose to remove any part of the sentence that is duplicated? – Scriptable Mar 16 '17 at 10:54
  • Actually, I need to retrieve not remove the similar part of the string. – pravid Mar 16 '17 at 10:56
  • _Is there any way to accomplish this with javascript or jQuery?_ Yes. Is it built in, no. You'll have to write a function yourself – George Mar 16 '17 at 10:57
  • Please update your question what you want. And make more explanation – Rana Ghosh Mar 16 '17 at 10:57
  • Do you ralize there might be more than one common part? In the example all the spaces can be seen as a single character common piece, so you would not know what to do exactly, unless you define what you want to happen. Anothere sample: "I think I am happy, I am sad, I am a tree, I am wind" with "I am what I am" – FrancescoMM Jan 21 '22 at 17:03

2 Answers2

3

You can use a simple for loop over the words and other character combinations array.

var str1 = "The first two have explicit values, but",
  str2 = "first two have explicit values, but disabled is empty";

// split two string by word boundary
var arr1 = str1.split(/\b/),
  arr2 = str2.split(/\b/);

// initialize variable for result
var str = '';

// iterate over the split array
for (var i = 0; i < arr1.length; i++) {
  // check current word includes in the array and check 
  // the combined word is in string, then concate with str
  if (arr2.includes(arr1[i]) && str2.indexOf(str + arr1[i]) > -1)
    str += arr1[i];
  // if string doesn't match and result length is greater
  // than 0 then break the loop
  else if (str.trim())
    break;
}

console.log(str.trim());
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
-3

Use replace on the string:

var match = 'first two have explicit values, but ';
var str1 = 'the first two have explicit values, but';
var str2 = 'first two have explicit values, but disabled is empty';

str1.replace(match, '')
// returns 'the first two have explicit values, but'

str2.replace(match, '')
// returns 'disabled is empty'

Note that if there are capitals in the string you will have to 'normalise' them, which is why the first check actually returns the original string (because there is no match). I suggest using toLowerCase on the strings for this.

developius
  • 1,193
  • 9
  • 17