javascript 在 jS 中减去两个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49497967/
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
Subtract two strings in jS
提问by Nithya S
I want to find the difference between two strings in Javascript.
我想在 Javascript 中找到两个字符串之间的区别。
Given two strings
给定两个字符串
var a = "<div>hello</div><div>hi</div><div>bye/</div>";
var b = "<div>hello</div><div>hi</div>";
The result should be "<div>bye</div>".
结果应该是 "<div>bye</div>".
Like in formula:
就像在公式中:
var result = a - b;
& I need this implementation in Javascript (Is there any default method is available for this in JS??)
&我需要在 Javascript 中实现这个(在 JS 中是否有可用的默认方法??)
Can anyone help me out?
谁能帮我吗?
回答by
You can obtain the desired output with
您可以获得所需的输出
var s = a.replace(b, '')
var s = a.replace(b, '')
回答by jojois74
This seems like an x/y question. But in any case, I'll try to help you out.
这似乎是一个x/y 问题。但无论如何,我会尽力帮助你。
We want to find the location of bwithin a.
我们希望找到的位置b内a。
var start = a.indexOf(b);
var end = start + b.length;
Now put it together.
现在把它放在一起。
return a.substring(0, start - 1) + a.substring(end);

