我正在格式化消息传递应用程序的文本。下划线等不同的字符串(例如http://link.com)。基本上,我需要分割以下内容:
"Hello example@email.com google is http://google.com http://google.com"
输出:
["Hello ", "example@email.com", " google is ", "http://google.com", "http://google.com"]
我的信息是:
"example@email.com", "http://google.com"
)这是我尝试过的:
const string = "Hello example@email.com google is http://google.com http://google.com";
const values = ["http://google.com", "example@email.com"];
// Create regex that finds the token values that we want to format
const regex = new RegExp(values.join('|'), 'g');
const splitted = text.split(regex);
=> ["Hello ", " google is "];
所以我所缺少的是拥有我想要的值。注意:这些值可以重复,我不知道顺序