通过regexp获取正确的字符串

时间:2017-07-16 13:18:21

标签: javascript jquery html

我有一个字符串的假设值:

//start string:
var text1 = 'Me & You';
var text2 = 'Me& You';
var text3 = 'Me &You';
var text4 = 'Me&You';
var text5 = 'Me        &You';
var text6 = 'Me&       You';
var text7 = 'Me     &  You';
//Final string I want to get:
text1: Me You
text2: Me You
text3: Me You
text4: MeYou
text5: Me        You
text6: Me       You
text7: Me       You
//real output:
text1: Me You
text2: Me You
text3: Me &You
text4: MeYou
text5: Me        //Here are many hidden spaces
//and similar outputs
//hypothetical RegExp
var string = 'Me     &  You';
let value = /\S*(&|#|%)\S*/;//for example
let resssC = string.split(value )...

如何获得所需的输出?这是regexp的问题还是还有什么问题?

2 个答案:

答案 0 :(得分:1)

我认为你必须只更换&

string.replace(/\&/g,"");

答案 1 :(得分:0)

也许就是这么简单:?

console.log(string.split("&").join(""))