我如何從這樣格式化的字串中獲取一些鍵值并將每個值保存到變數中?
[Alert] <value here> has thrown a <value here> in <value here>.
我真的不知道該怎么做......也許是正則運算式?我對正則運算式不是很熟悉。
uj5u.com熱心網友回復:
正則運算式非常簡單,幾乎可以與您的字串相同。如果您想挑選一些單詞,請使用匹配語法(. )- 句點是“任何字符”,而 是“一個或多個”。
在帶有運算式的字串上使用match以回傳一個匹配陣列,然后您可以將其解構為許多變數(我在這里a b和c這里都呼叫過它們)。
// Note: you have to escape the `[` and `]` around "Alert"
// as they are part of regex syntax. `^` and `$`
// signify the start and end of the string respectively
const re = /^\[Alert\] (. ) has thrown a (. ) in (. )\.$/;
const str = '[Alert] Trump has thrown a tantrum in his Evil Lair.'
const [full, a, b, c] = str.match(re);
console.log(a, b, c);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/510273.html
下一篇:獲取R中許多檔案夾中的檔案名
