我想从这个字符中抓取粗体文本:example.com/en/en/some-text/test%C5%84sk/thisparttofchariwant-thisnot-not/不是
我创建了:
const myArr = str.split("/")[5];
但它是返回我:
thispartofcharIwant-thisnot-not/not
所以这不是我的目标,目标是得到这个:
thispartofcharIwant
let str = 'example.com/en/en/some-text/test%C5%84sk/thispartofcharIwant-thisnot-not/not'
const part = str.split("/")[5].split("-")[0]
// const [part] = str.split("/")[5].split("-"); //alternative way using destructuring
console.log(part) // 'thispartofcharIwant'
最简单的解决方法是再次拆分并解构生成的数组:
const haystack = "example.com/en/en/some-text/test%C5%84sk/thispartofcharIwant-thisnot-not/not";
const [needle] = haystack.split("/")[5].split("-");
console.log(needle);