我想抓住從這個字符只粗體文字:example.com/en/en/some-text/testńsk/ thispartofcharIwant -thisnot而不是/不
我創造了:
const myArr = str.split("/")[5];
但它回傳給我:
thispartofcharIwant-thisnot-not/not
所以這不是我的目標,目標是得到這個:
thispartofcharIwant
uj5u.com熱心網友回復:
let str = 'example.com/en/en/some-text/testńsk/thispartofcharIwant-thisnot-not/not'
const myArr = str.split("/")[5].split("-")[0]
console.log(myArr) // 'thispartofcharIwant'
uj5u.com熱心網友回復:
最簡單的解決方案是再次拆分并解構結果陣列:
const haystack = "example.com/en/en/some-text/testńsk/thispartofcharIwant-thisnot-not/not";
const [needle] = haystack.split("/")[5].split("-");
console.log(needle);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/339333.html
標籤:javascript
