Shoutbox

[Help]Returning the inside of a string... - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [Help]Returning the inside of a string... (/showthread.php?tid=63118)

[Help]Returning the inside of a string... by andrewdodd13 on 07-12-2006 at 05:11 PM

I'm developing a mod script atm which adds extra bb code tags (wont say what it is atm) but I've run into a little problem. I was originally planing to use match and substr to try this, but I'm hoping there's a better way.

What I'm stuck with is actually getting the text from inside the tags, for example the inside of [tag]hi[/tag] would be hi.

Anyone know an easy way to do this? (And that'll save me having to read up on Regular Expressions |-))


RE: [Help]Returning the inside of a string... by Pai on 07-12-2006 at 06:01 PM

To extract the inside of [tag]hi[/tag]:

code:
var tagInside = tags.replace(/\[tag\]|\[\/tag\]/gi,"");

result: tagInside contains "hi"
RE: [Help]Returning the inside of a string... by -dt- on 07-13-2006 at 03:13 AM

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


RE: RE: [Help]Returning the inside of a string... by Pai on 07-13-2006 at 08:32 AM

quote:
code:
/* 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...
*/



That would be correct if we were using PHP (preg), but doesn't apply to JScript ... text only contains a string with the matched expression ([b]moo[/b] in this case), and not an array with the matches :S

Test case:
code:
var Message = "this is just a [b]test[/b] string that doesnt make [u] any sense [/u] ..";
var text = Message.match(/\[b\](.*?)\[\/b\](.*?)\[u\](.*?)\[\/u\]/g);

Result:
  text[0] or text: [b]test[/b] string that doesnt make [u] any sense [/u]
text[1,2,3,...,N]: null


RE: [Help]Returning the inside of a string... by Silentdragon on 07-13-2006 at 09:56 AM

It doesn't work for you because the way you wrote the RegExp. It will return an array of matches.

Your code should be like this

code:
var text = Message.match(/((\[b\](.*?)\[\/b\](.*?))|(\[u\](.*?)\[\/u\]))/g);

This should work as well
code:
var sMessage = "[n]stuff[/n] hey wow [n]more stuff[/n]";
var NoParse = sMessage.match(/\[n\].+?\[\/n\]/mgi);

Your results would be
NoParse[0] = [n]stuff[/n]
NoParse[1] = [n]more stuff[/n]

RE: [Help]Returning the inside of a string... by -dt- on 07-13-2006 at 10:07 AM

quote:
Originally posted by Pai
That would be correct if we were using PHP (preg), but doesn't apply to JScript ... text only contains a string with the matched expression (moo in this case), and not an array with the matches (Smilie)
it does return captured text ( stuff within () ) if you dont use the G modifier which is something I forgot about in my example


eg javascript shell output

code:

//first test
"[b]moo[/b][b]aaa[/b]".match(/\[b\](.*?)\[\/b\]/);
-> [b]moo[/b],moo


//with G
"[b]moo[/b][b]aaa[/b]".match(/\[b\](.*?)\[\/b\]/g);
-> [b]moo[/b],[b]aaa[/b]



RE: [Help]Returning the inside of a string... by Pai on 07-13-2006 at 11:21 AM

Uhmm without the g works like a charm yes :P

Well thanks I guess, even tho it's not my thread I learned something :)


RE: [Help]Returning the inside of a string... by andrewdodd13 on 07-13-2006 at 11:33 AM

Thanks guys, I think I've got the hang of it now. -dt-'s script worked pretty much straight off once I took the \g out to \. <-- (Edit: That period is the end of the sentence, not \.)