08-02-2002, 11: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>
<small> ... anything ... </small>
08-02-2002, 03:16 PM
newstr = str.replace(/^(.*)<span class="small">(.*)<\/span>(.*)$/, "$1<small>$2<\/small>$3");
08-02-2002, 03:38 PM
08-02-2002, 03:42 PM
I don't understand. What would you like that would be different?
08-02-2002, 03: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.
08-02-2002, 05:45 PM
i don't know how familiar you are with RE's, so here's a complete breakdown of it.
^ 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:
someone please let me know if that's wrong.
08-03-2002, 01: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 :)
08-05-2002, 03: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).