Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Location
    Milwaukee, Wisconsin
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts

    ugh im having some troubles with regular expressions.

    well im making a WYSIWYG editor out of javascript and im making part of it where it translates it form the wysiwyg into BBCode(its for a forum system im working on) k. well for some reason i cant seem to get my replace() function to work. sortof... heres my expression:
    html = html.replace(/<(img|IMG) (src|SRC)=\"(.*)\"><\/(img|IMG)>/gi, "[$1]$3[/$1]");

    what i think this is doing is finding the IMG tag then when it finds that and it see source, where the (.*) is its grabbing every other image in the text box, i think, so i dunno i think i somehow need to make it stop once it sees the first </img> tag when i try to use (?atter) or (pattern) it finds the pattern as if it were going backwards.. like if i was looking for Z in abZdZfg

    it would find the second Z first. if anyone could help that would br=e great, thanx again guys and girls.



    also if you wanted to see the editor here ya go:
    http://stoodder.50free.org/javascripts/editor.html

  • #2
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not sure of what u mean,but to give you a good adivce,the best thing for the editing surface,that is where the users type their text,is a <div contentEditable="true">

  • #3
    Regular Coder
    Join Date
    Oct 2002
    Location
    Milwaukee, Wisconsin
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    yup lol thanx for the advice, i already have my content editable set thoug, okay so what i mean is i want limit certain html codes only. what i want to do is say the user inserts an image the images html should be like: <IMG src="whatever"> right? well what i want to do is be able to include everytag in the middle(if the user were to resize the image) what i use is html = html.replace(/<IMG(.*)src=(.*)>(<\/IMG>)/gi, "[IMG$1]$2[/IMG]");

    but for some reason say tehre was two images like:

    <img src=blah><img src=yahoo>

    it would find the first image but the first images source would eand at o on the second... what its doing is includeing everything else until it finds the very last occurance of like > ro whatever, s im wondering if ther a way to make a global function to check the fitst accourance then stop, any ideas?

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    (.*) means "any amount of anything" - that's why your regexes go to far - they're too greedy.

    Firstly you should quote your attributes properly - then it's much easier. Given that, what you want is something like this:

    <IMG[ ]+src="[^\"]*">

    Which will match IMG followed by one or more spaces, followed by src=", then any amount of anything*except* a double-quote (including nothing), then another double-quote and close tag
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #5
    Regular Coder
    Join Date
    Oct 2002
    Location
    Milwaukee, Wisconsin
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanx man, im really not fodd of regular expressions lol

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There's a book by O'Reilly called "Mastering Regular Expressions". It changed my life mate
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #7
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by brothercake
    There's a book by O'Reilly called "Mastering Regular Expressions".
    That's usually referred to as simply "The Owl Book", correct? It's considered by many people to be the best book on regular expressions. Maybe I'll get it someday.

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ISBN 1-56592-257-3 - it does have an Owl on it.
    Check out the Forum Search. It's the short path to getting great results from this forum.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •