자바 정규식 닌자에 대한 질문이 있습니다. 어떻게 정규식 그룹화를 사용하여 문자열에서 변수 생성을 단순화 할 수 있습니까? 현재 그룹화를 사용하지 않고 작업하고 있지만 더 좋은 방법을보고 싶습니다!Javascript Regex 여러 그룹과 패턴이 일치합니다.
문자열은 다음과 같습니다 작동
var url = 'resources/css/main.css?detect=#information{width:300px;}';
코드는 다음과 같습니다
var styleStr = /[^=]+$/.exec(url).toString();
var id = /[^\#][^\{]+/.exec(styleStr).toString();
var property = /[^\{]+/.exec(/[^\#\w][^\:]+/.exec(styleStr)).toString();
var value = /[^\:]+/.exec(/[^\#\w\{][^\:]+[^\;\}]/.exec(styleStr)).toString();
이 제공 :
alert(id) //information
alert(property) //width
alert(value) //300px
모든 응시자?
는 CSS 선택기 문법은 약간 더 복잡하다. (http://www.w3.org/TR/CSS2/grammar.html 참조) – Gumbo