...

View Full Version : ugh im having some troubles with regular expressions.



stoodder
01-01-2004, 11:26 PM
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 (?:patter) 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

Code Wizard
01-02-2004, 02:01 PM
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">

stoodder
01-03-2004, 04:00 PM
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, "$2");

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?

brothercake
01-03-2004, 07:24 PM
(.*) 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

stoodder
01-03-2004, 10:27 PM
thanx man, im really not fodd of regular expressions lol

brothercake
01-04-2004, 01:43 PM
There's a book by O'Reilly called "Mastering Regular Expressions". It changed my life mate :)

fredmv
01-05-2004, 06:04 AM
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.

Roy Sinclair
01-05-2004, 09:44 PM
ISBN 1-56592-257-3 - it does have an Owl on it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum