使用JavaScript使用对象分割和替换字符串

时间:2019-07-10 04:54:35

标签: javascript jquery html regex

我想知道是否有任何其他有效的方法来分割和替换javascript中的字符串;

var str= "/go-where" or "/go"
var repl = "to"
var result = splitandreplace(str);

function splitandreplace(val, repl){
  var x= str.split("/")[1];
  var res= x.split("-")[0] + x.split('-')[1]!==undefined ? x.split('-')[1]=(en) :''; // will be to(en)
}

Expected Output:
when str is 
/go -> to
/go-where -> to(en)

1 个答案:

答案 0 :(得分:0)

具有正则表达式的解决方案:

var str = "<a href=\"https://twitter.com/search?q=fnb\" target=\"_blank\">#fnb</a>, <a href=\"https://twitter.com/search?q=mobilesimcard\" target=\"_blank\">#mobilesimcard</a>, <a href=\"https://twitter.com/search?q=what\" target=\"_blank\">#what</a>, <a href=\"https://twitter.com/search?q=refugeechild\" target=\"_blank\">#refugeechild</a>";
var array = str.match(/#[a-z-_]+/ig)

console.log(array);