我有以下字串,
He is @(role)
我需要獲取@(和之間存在的字串)。
預期結果 ,
role
uj5u.com熱心網友回復:
我們可以match()在這里使用:
var input = "He is @(role)";
var role = input.match(/@\((.*?)\)/)[1];
console.log(role);
uj5u.com熱心網友回復:
您必須使用 string 的 split 方法將字串拆分到您想要的位置,在這里回答問題:
const string = '他是@(角色)';
常量答案 = string.split('@');
如果你 console.log(answer) 然后控制臺顯示
['他是','(角色)'];
uj5u.com熱心網友回復:
const arrMatch = 'He is @(role)'.match(/\@\(([^\)] )/)
const text = arrMatch[1]
console.log(text)
uj5u.com熱心網友回復:
這是使用字串切片方法執行此操作的最佳且簡單的方法,該方法可幫助您拼接所需的確切字串。這是答案
const string = '他是@(角色)';
常量答案 = string.slice(8, 12);
console.log(answer);
現在您可以看到結果“角色”
說明:這是幫助您學習此方法的資源
https://www.w3schools.com/jsref/jsref_slice_string.asp#:~:text=The slice() method extracts,of the string to extract。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/505607.html
標籤:javascript 正则表达式 分裂
