View Full Version : looking for a regular expression
brothercake
08-02-2002, 10:20 AM
All the expression I meet are nutters ...
No anyway - I'm hoping someone with a good grasp of regexp can help me with this. Basically, I want to be to replace predictable pairs of HTML tags with different tags, without affecting what's between them. So for example
<span class="small"> ... anything ... </span>
could become
<small> ... anything ... </small>
jalarie
08-02-2002, 02:16 PM
str='before<span class="small">during</span>after';
newstr = str.replace(/^(.*)<span class="small">(.*)<\/span>(.*)$/, "$1<small>$2<\/small>$3");
alert(newstr);
brothercake
08-02-2002, 02:38 PM
Thanks; that's the business, but I was hoping for a general regexp rather than a javascript expression - something I can use in a search/replace text editor. Co0uld you help modifying it?
jalarie
08-02-2002, 02:42 PM
I don't understand. What would you like that would be different?
SYP}{ER
08-02-2002, 02:43 PM
Then I think just /^(.*)<span class="small">(.*)<\/span>(.*)/ would work in finding a match...
I didn't think it was possible to replace stuff around any data without losing that data... I've always just replaced one side and then the other.
joh6nn
08-02-2002, 04:45 PM
i don't know how familiar you are with RE's, so here's a complete breakdown of it.
/^(.*)<span class="small">(.*)<\/span>(.*)$/$1<small>$2<\/small>$3/
^ is the start of the string, and (.*) means 0 or more of any character. so it's string start, everything but the HTML, everything inside the HTML, the end of the HTML, then everything else, then the end of the string. in this case, the start of the string would be the start of the file (or the start of the line, but i think in a text editor it would be the start of the file) and the end of the string would be the end of the file.
then, the second part of the RE is what should be used to replace the matching text. in a RE, when you enclose things in parenthesis, they get grouped together, and assigned a number, so you can reference that group later. hence the $1, $2, and $3. that all translates to everything that was before the old html, the new html, everything that was between the old html, the new html, everything that was after the old html.
it seems to me like you're looking to replace bunches of occurrences of this string throughout a file. i don't know if that's real;y what you want, and i don't know if Jalarie's would do that. i'm far from an expert with RE's, but i think that Jalarie's would match one occurrence within the file. as i understand it, to match multiple occurrences, you'd need the following:
/<span class="small">(.*)<\/span>/<small>$1<\/small>/gi
someone please let me know if that's wrong.
brothercake
08-03-2002, 12:09 PM
Yeah, I was looking for something I can use in a search and replace editor to change all occurences of that tag in a whole site, and I'm also not at familiar with reg expressions.
so thanks all - very helpful :)
mordred
08-05-2002, 02:53 PM
If you try to use the RegExp above in a global search-and-replace action, beware of the greediness of the quantifiers. They might match more than you wanted to.
And don't forget to make a backup of your original file in case anything goes wrong (an advice from someone who didn't do it during his first exposure to RegExp and hated himself for not thinking about that beforehand).
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.