What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [Help]Returning the inside of a string...

[Help]Returning the inside of a string...
Author: Message:
andrewdodd13
Senior Member
****

Avatar
Oh so retro

Posts: 870
Reputation: 16
34 / Male / Flag
Joined: Jan 2005
O.P. [Help]Returning the inside of a string...
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 |-))
[Image: AndrewsStyle.png]
07-12-2006 05:11 PM
Profile E-Mail PM Web Find Quote Report
Pai
Full Member
***

w00t !

Posts: 203
Reputation: 2
– / Male / –
Joined: Sep 2003
RE: [Help]Returning the inside of a string...
To extract the inside of [tag]hi[/tag]:

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

result: tagInside contains "hi"
07-12-2006 06:01 PM
Profile PM Find Quote Report
-dt-
Scripting Contest Winner
*****

Avatar
;o

Posts: 1819
Reputation: 74
35 / Male / Flag
Joined: Mar 2004
RE: [Help]Returning the inside of a string...
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

[Image: dt2.0v2.png]      Happy Birthday, WDZ
07-13-2006 03:13 AM
Profile PM Web Find Quote Report
Pai
Full Member
***

w00t !

Posts: 203
Reputation: 2
– / Male / –
Joined: Sep 2003
RE: RE: [Help]Returning the inside of a string...
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

07-13-2006 08:32 AM
Profile PM Find Quote Report
Silentdragon
Full Member
***

Avatar
if(life==null && wrists) EmoAlert();

Posts: 148
Reputation: 2
34 / Male / –
Joined: Jun 2006
RE: [Help]Returning the inside of a string...
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]

This post was edited on 07-13-2006 at 09:58 AM by Silentdragon.
07-13-2006 09:56 AM
Profile E-Mail PM Web Find Quote Report
-dt-
Scripting Contest Winner
*****

Avatar
;o

Posts: 1819
Reputation: 74
35 / Male / Flag
Joined: Mar 2004
RE: [Help]Returning the inside of a string...
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]



This post was edited on 07-13-2006 at 10:07 AM by -dt-.
[Image: dt2.0v2.png]      Happy Birthday, WDZ
07-13-2006 10:07 AM
Profile PM Web Find Quote Report
Pai
Full Member
***

w00t !

Posts: 203
Reputation: 2
– / Male / –
Joined: Sep 2003
RE: [Help]Returning the inside of a string...
Uhmm without the g works like a charm yes :P

Well thanks I guess, even tho it's not my thread I learned something :)
07-13-2006 11:21 AM
Profile PM Find Quote Report
andrewdodd13
Senior Member
****

Avatar
Oh so retro

Posts: 870
Reputation: 16
34 / Male / Flag
Joined: Jan 2005
O.P. RE: [Help]Returning the inside of a string...
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 \.)

This post was edited on 07-13-2006 at 11:34 AM by andrewdodd13.
[Image: AndrewsStyle.png]
07-13-2006 11:33 AM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On