[英]Javascript regex error, text between two characters
(?<=\[)[^\]]*(?=\])
匹配文本:
[11/Sep/2016:21:58:55 +0000]
在測試時,它在升華方面效果很好,但是當我這樣做時
str.match(/(?<=\[)[^\]]*(?=\])/)
Ive錯誤:SyntaxError:正則表達式無效
我在做什么錯?
您可以將regex用作: / [^ \\ [\\]] + /
const regex = /[^\\[\\]]+/; const str = `[11/Sep/2016:21:58:55 +0000]`; let m; if ((m = regex.exec(str)) !== null) { // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }
您可以使用捕獲並獲取第1組的內容。
如果您要提取一個值,請使用
var m = "[11/Sep/2016:21:58:55 +0000]".match(/\\[([^\\]]*)]/); console.log(m ? m[1] : "No match");
如果還有更多, RegExp#exec
與/\\[([^\\]]*)]/g
並收集匹配項:
var s = "[11/Sep/2016:21:58:55 +0000] [12/Oct/2016:20:58:55 +0001]"; var rx = /\\[([^\\]]*)]/g; var res = []; while ((m=rx.exec(s)) !== null) { res.push(m[1]); } console.log(res);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.