quote:
Originally posted by Pai
To extract the inside of [tag]hi[/tag]:
code:
var tagInside = tags.replace(/\[tag\]|\[\/tag\]/gi,"");
result: tagInside contains "hi"
thats a horrible way IMO , what happens if the string isnt just "[tag]hi[/tag]" we should use regular expressions and use String::match
code:
var Message = "the cow goes [b]moo[/b] in the morning";
//now theres a few things we can do... extract the text like so...
var text = Message.match(/\[b\](.*?)\[\/b\]/g);
/* text is an array
text[0] will contain the full matched string eg [b]moo[/b]
text[1] will contain the first match eg moo
if there was another match it would be in text[2] and so on...
*/
//we could replace the [b] tags with other tags eg <b>
Message = Message.replace(/\[b\]/g, "<b>");
Message = Message.replace(/\[\/b\]/g, "</b>");
A few helpful links
A regexp index
A regexp tuturial
Another regexp tuturial
Another regexp Tutorial ment for php but theres alot of useful information there