Javascript 使用正则表达式查找Javascript中两个字符串之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29573700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding the difference between two string in Javascript with regex
提问by techguy2000
Regex experts please help to see if this problem can be solved by regex:
请正则表达式高手帮忙看看这个问题是否可以通过正则表达式解决:
Given string 1 is any string
给定字符串 1 是任何字符串
And string 2 is any string containing all parts of string 1 (but not a simple match -- I will give example)
字符串 2 是包含字符串 1 的所有部分的任何字符串(但不是简单的匹配——我举个例子)
How to use regex to replace all parts of string 1 in string 2 with blank so that what's remained is the string not in string 1?
如何使用正则表达式将字符串 2 中字符串 1 的所有部分替换为空白,以便剩下的字符串不在字符串 1 中?
For example: str1 = "test xyz"; str2 = "test ab xyz"
例如:str1 = "test xyz"; str2 = "测试 ab xyz"
I want " ab" or "ab " back. What is the regex I can write so that when I run a replace function on str2, it will return " ab"?
我想要“ab”或“ab”回来。我可以编写什么正则表达式,以便在 str2 上运行替换函数时,它会返回“ab”?
Here is some non-regex code:
这是一些非正则表达式代码:
function findStringDiff(str1, str2) {
var compareString = function(str1, str2) {
var a1 = str1.split("");
var a2 = str2.split("");
var idx2 = 0;
a1.forEach(function(val) {
if (a2[idx2] === val) {
a2.splice(idx2,1);
} else {
idx2 += 1;
}
});
if (idx2 > 0) {
a2.splice(idx2,a2.length);
}
return a2.join("");
}
if (str1.length < str2.length) {
return compareString(str1, str2);
} else {
return compareString(str2, str1);
}
}
console.log(findStringDiff("test xyz","test ab xyz"));
回答by Millie Smith
Regexes only recognize if a string matches a certain pattern. They're not flexible enough to do comparisons like you're asking for. You would have to take the first string and build a regular language based on it to recognize the second string, and then use match groups to grab the other parts of the second string and concatenate them together. Here's something that does what I think you want in a readable way.
正则表达式仅识别字符串是否与特定模式匹配。它们不够灵活,无法像您要求的那样进行比较。您必须采用第一个字符串并基于它构建一种常规语言来识别第二个字符串,然后使用匹配组来获取第二个字符串的其他部分并将它们连接在一起。这是一些以可读的方式完成我认为你想要的东西。
//assuming "b" contains a subsequence containing
//all of the letters in "a" in the same order
function getDifference(a, b)
{
var i = 0;
var j = 0;
var result = "";
while (j < b.length)
{
if (a[i] != b[j] || i == a.length)
result += b[j];
else
i++;
j++;
}
return result;
}
console.log(getDifference("test fly", "test xy flry"));
Here's a jsfiddle for it: http://jsfiddle.net/d4rcuxw9/1/
这是一个 jsfiddle:http: //jsfiddle.net/d4rcuxw9/1/
回答by Lorenz Meyer
I find this question really interesting. Even though I'm a little late, I would like to share my solution on how to accomplish this with regex. The solution is concise but not very readable.
我觉得这个问题真的很有趣。尽管我来晚了一点,但我想分享我的解决方案,了解如何使用正则表达式完成此任务。该解决方案简洁但可读性不强。
While I like it for its conciseness, I probably would not use it my code, because it's opacity reduces the maintainability.
虽然我喜欢它的简洁性,但我可能不会在我的代码中使用它,因为它的不透明度降低了可维护性。
var str1 = "test xyz",
str2 = "test ab xyz"
replacement = '';
var regex = new RegExp(str1.split('').map(function(char){
return char.replace(/[.(){}+*?[|\]\^$]/, '\$&');
}).join('(.*)'));
if(regex.test(str2)){
for(i=1; i<str1.length; i++) replacement = replacement.concat('$' + i);
var difference = str2.replace(regex, replacement);
} else {
alert ('str2 does not contain str1');
}
The regular expression for "test xyz"is /t(.*)e(.*)s(.*)t(.*) (.*)x(.*)y(.*)z/and replacementis "$1$2$3$4$5$6$7".
"test xyz"is/t(.*)e(.*)s(.*)t(.*) (.*)x(.*)y(.*)z/和replacementis的正则表达式"$1$2$3$4$5$6$7"。
The code is no longer concise, but it works now even if str1 contains special characters.
代码不再简洁,但即使 str1 包含特殊字符,它现在也能工作。
回答by James Wilkins
To find out if there are extra '.' like you are asking for, you can do this:
找出是否有额外的'.' 就像您要求的那样,您可以这样做:
result = "...00".match(/$1\.(\.*)?00/)[1];
resultis then the EXTRA '.'s found. You cannot use regex to compare strings using only regex. Perhaps use this, then compare the results.
result然后是 EXTRA '.'s 找到。您不能使用正则表达式仅使用正则表达式来比较字符串。也许使用这个,然后比较结果。
You can also try this:
你也可以试试这个:
result = "...00".match(/($)(\d+)\.(\.*)?(\d+)/);
// Outputs: ["...00", "$", "1", "..", "00"]
Which will extract the various parts to compare.
这将提取各个部分进行比较。
回答by gfullam
If you are only concerned with testing whether a given string contains two or more sequential dot '.' characters:
如果您只关心测试给定的字符串是否包含两个或多个连续的点 '.' 人物:
var string = '..00',
regexp = /(\.\.+)/;
alert('Is this regular expression ' + regexp + ' found in this string ' + string + '?\n\n' + regexp.test(string) + '\n\n' + 'Match and captures: ' + regexp.exec(string));
If you need it to match the currency format:
如果您需要它来匹配货币格式:
var string = '..00',
regexp = /$\d*(\.\.+)(?:\d\d)+/;
alert('Is this regular expression ' + regexp + ' found in this string ' + string + '?\n\n' + regexp.test(string) + '\n\n' + 'Match and captures: ' + regexp.exec(string));
But I caution you that Regular Expressions aren't for comparing the differences between two strings; they are used for defining patterns to match against given strings.
但我提醒您,正则表达式不是用于比较两个字符串之间的差异;它们用于定义匹配给定字符串的模式。
So, while this may directly answer how to find the "multiple dots" pattern, it is useless for "finding the difference between two strings".
因此,虽然这可能直接回答如何找到“多个点”模式,但对于“找到两个字符串之间的差异”是没有用的。
The StackOverflow tag wiki provides an excellent overview and basic reference for RegEx. See: https://stackoverflow.com/tags/regex/info
StackOverflow 标签 wiki 为 RegEx 提供了出色的概述和基本参考。请参阅:https: //stackoverflow.com/tags/regex/info

