2016-11-16 18 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

文字列置換現在の文字列を実際に更新するのではなく、置換が適用された新しい文字列を返します。だから、

propertiesText = propertiesText.replace(regexExpression, newString); 

にあなたのコードの更新を修正するreplace()メソッドは、指定された値の文字列、または正規表現を検索し、指定された値である新しい文字列を返すhttp://www.w3schools.com/jsref/jsref_replace.asp

を参照してください。置き換えられました。

+0

私は実際に置き換えられたinstを疑っていましたが、Regexが問題であったと私はあまりにも盲目でした。残っていることは、大きな**ありがとうございます**と言うことです。 –

関連する問題