开发者

String.replace() function to parse XML string so that it can be displayed in HTML

I have a XML string which needs to be displayed within HTML. I understand the first thing needed to be done here is to convert all '<' and '>' into '& lt;' and '& gt;' (ignore the space after & sign). This is what I am doing to replace '<' -

regExp = new RegExp("/</g");
xmlString = xmlString.replace(regExp, '& lt;');

xmlString does 开发者_Go百科not change.

Also, trace(regExp.test("<")); prints false.

What is wrong here?


replace returns a new string, it doesn't modify the old one. So if you want to overwrite the old you have to do the following:

xmlString = xmlString.replace(regExp, '&lt;');

Or if you don't want to overwrite the old one, just store the result in a new variable.

var newString = xmlString.replace(regExp, '&lt;');


The issue is the way you create your RegExp object.

Because your using the RegExp constructor, don't include the / characters:

regExp = new RegExp("<", "g");

or use / as a shortcut:

regExp = /</g;

See this page for more details: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/RegExp.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜