2016-11-16 1 views
0

textarea 텍스트의 일부를 regex로 대체하려고합니다. 정규 표현식이 일치하지 않습니다

propertiesText

textarea로부터받은 전체 텍스트이며, 대체 코드는 다음과 같다 :

Console Regex

: 콘솔에서
var propertyName = inputs[i].name; 
var propertyValue = inputs[i].value; 

//#regexExpression = #propertyName=.* [example: /#EPCDatabase\/EPCdatabase.param\/epc.db.user=.*$/gim]// 
var regexExpression = new RegExp(propertyName + "=.*$","gim"); 
//#newString = propertyName=propertyValue [example: EPCDatabase\/EPCdatabase.param\/epc.db.user=ddddd]// 
var newString = propertyName.substring(1) + "=" + propertyValue; 
console.log("Regex Expression: " + regexExpression); 
console.log("Replace String: " + newString); 

propertiesText.replace(regexExpression, newString); 
console.log(propertiesText); 
return; 

, 나는 다음과 같은 정규식 표현을 얻고있다

텍스트는 원본에서 바뀌지 않습니다 propertiesText :

Properties String after replace

정규식과 you can see it is matching을 확인해 보았습니다.

나는 또한 출력 된 동일한 정규식과 대체 코드 부분을 분리하려고 시도했으며 as you can see again이라는 작업을 수행했습니다.

코드에 무엇이 누락 되었습니까?

답변

3

문자열 바꾸기 현재 문자열을 acutally 업데이트하지만 단순히 바꾸기가 적용된 새 문자열을 반환합니다. 그래서 메소드가 지정된 값은 새 문자열을 지정된 값, 또는 정규 표현식에 대한 문자열을 검색하고 반환() http://www.w3schools.com/jsref/jsref_replace.asp

가 교체를 참조하십시오

propertiesText = propertiesText.replace(regexExpression, newString); 

에 코드 업데이트를 해결하는 대체 됨.

+0

나는 대체 수술실을 실제로 대체 할 것으로 의심 했었지만 정규식 문제가 너무 장님이라고 생각했습니다. 남은 것은 큰 ** 고마워요 **입니다. –

관련 문제