2017-05-11 5 views

답변

2

@loretoparisi와 같지만 그룹을 덮어 씁니다.

+0

바로 그 점을 잊어 버렸습니다! – loretoparisi

5

요 괄호 사이의 모든 경기를 얻기 위해 정규식 /\[(.*?)\]/g를 사용하고 마지막으로 일치하는 경우 수행 할 수 있습니다

str = "foo[biz][bar]" 
 
matches = str.match(/\[(.*?)\]/g) 
 
if (matches.length) console.log(matches[matches.length - 1]) 
 
// based on answer above group override but without `(?:` non capturing group 
 
console.log(/(\[(\w+)\])+/g.exec(str).pop())

정규식 /\[(.*?)\]/g 설명 : enter image description here

정규식을 /(\[(\w+)\])+/g 설명 : enter image description here

작성자 : Debuggex

+2

당신이 패턴을 수행하는 방법을 설명 할 수 작업? 나는 패턴이'/\((.*)\]/g' 일 때''[biz] [bar] ''를 얻는 이유를 이해합니다. '? '는'/\[((()(,)\]/g'에서 이전 결과를 두 부분으로 나눕니다. '?'는 무엇의 "0 또는 1"을 의미합니까? – pmichna

+1

설명을 추가했습니다. – loretoparisi