我有一个特定的类型网址,我需要识别它的某些部分并替换为一些数据,网址为www.something.com@param1={{^User Name^}},param2={{^user id^}}
,....
我需要做的是,识别{{^User Name^}}
和{{^user id^}}
并替换为我的值,
有人有想法用flex做这个吗?
我的一位朋友告诉我使用RegExp,但我对如何使用它一无所知。
答案 0 :(得分:1)
像这样的东西可能有用,没有RegExp的专家。
var s:String = "www.something.com@param1={{^User Name^}},param2={{^user id^}}";
var userPattern:RegExp = /(?<=param1=)({{\^)([A-Za-z0-9 ])*(\^}})/;
var userIdPattern:RegExp = /(?<=param2=)({{\^)([A-Za-z0-9 ])*(\^}})/;
s = s.replace(userPattern,'username');
s = s.replace(userIdPattern,'user_id');
如果你只想替换{{}} maby里面的字符串,那就行了:
var s:String = "www.something.com@param1={{^User Name^}},param2={{^user id^}}";
var userPattern:RegExp = /(?<=param1={{)\^([A-Za-z0-9 ])*\^(?=}})/;
var userIdPattern:RegExp = /(?<=param2={{)\^([A-Za-z0-9 ])*\^(?=}})/;
s = s.replace(userPattern,'username');
s = s.replace(userIdPattern,'user_id');
要删除多个{{}}例如www.something.com@param1={{^User Name ^}},{{^,e,^}},param2 = {{^ user id ^}}之间paramX试试这个:
var s:String = "www.something.com@param1={{^User Name^}},{{^,e,^}},param2={{^user id^}}";
var userPattern:RegExp = /(?<=param1={{)\^([A-Za-z0-9\^}}{{, ])*(?=}})/;
var userIdPattern:RegExp = /(?<=param2={{)\^([A-Za-z0-9\^}}{{, ])*(?=}})/;
s = s.replace(userPattern,'username');
s = s.replace(userIdPattern,'user_id');
输出应为www.something.com@param1 = {{username}},param2 = {{user_id}}。
另一个例子:
var s:String = "www.something.com@param1={{^User Name^}},{{^,e,^}},param2={{^user id^}}";
var userPattern:RegExp = /(?<=param1=)({{\^)([A-Za-z0-9\^}}{{, ])*(})/;
var userIdPattern:RegExp = /(?<=param2=)({{\^)([A-Za-z0-9\^}}{{, ])*(})/;
s = s.replace(userPattern,'username');
s = s.replace(userIdPattern,'user_id');
输出应为www.something.com @ param1 = username,param2 = user_id。
答案 1 :(得分:0)
也许不是最有效或最好的解决方案,但它会完成工作。 只需更新replace()函数即可管理不同类型的参数。
public static const START_TAG:String = "{{^";
public static const END_TAG:String = "^}}";
public function replace():void {
var url:String = "param1={{^User Name^}},param2={{^user id^}}";
while ( getTagStartIndex(url) >= 0 ) {
var currentTag:String = getTagContent(url);
var value:String = "";
if ( currentTag == "User Name" ) {
value = "myName";
} else if ( currentTag == "user id" ) {
value = "myID";
} else {
value = "undefinedtag";
}
url = replaceTagWithValue(currentTag,value, url);
}
}
protected function getTagStartIndex(str:String):int {
return str.indexOf(START_TAG);
}
protected function getTagEndIndex(str:String):int {
return str.indexOf(END_TAG, getTagStartIndex(str+1));
}
protected function getTagContent(str:String):String {
return str.substring(getTagStartIndex(str)+START_TAG.length, getTagEndIndex(str));
}
protected function replaceTagWithValue(tag:String, tagValue:String, str:String):String {
return str.replace(START_TAG+tag+END_TAG,tagValue);
}
答案 2 :(得分:0)