Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Regular Expressions + JavaScript

  • 28-05-2008 02:57PM
    #1
    Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭


    Hi folks,

    I'm having trouble getting my head around regular expressions. I'm trying to convert all HTML/XML tags to lowercase using JavaScript.

    I've gotten as far as this, but can't figure out what I need to replace the question marks with to convert the contents of the tag to lowercase...
    var sString = "<B>text</B>";
    sString.replace(/<(.|\n)+?>/g, ???);
    

    thanks in advance.


Comments

  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    I think the obvious question is .. "Why?". HTML is not case sensitive.

    Anyway regexp doesn't work that way. It matches and can replace but you couldn't do a complex replace (that I am aware of).

    your expression should be like.
    /<[\/ ]*([A-Z]+)[ >]/g
    
    / = start
    < = match literal <
    [\/ ]* = Match / or space 0 or unlimited number of times.
    ([A-Z]+) = Match A to Z any number of times and group into group 1. 
    [ >] = Match space or > at least once.
    /g = End. (global search).
    

    Should catch most stuff. Won't find the variables in the tag though.

    After that do a toLower on the group found and send it back in. Probably an easier way to do this via the DOM though.


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    Hobbes wrote: »
    I think the obvious question is .. "Why?". HTML is not case sensitive.

    Anyway regexp doesn't work that way. It matches and can replace but you couldn't do a complex replace (that I am aware of).

    your expression should be like.
    /<[\/ ]*([A-Z]+)[ >]/g
    
    / = start
    < = match literal <
    [\/ ]* = Match / or space 0 or unlimited number of times.
    ([A-Z]+) = Match A to Z any number of times and group into group 1. 
    [ >] = Match space or > at least once.
    /g = End. (global search).
    

    Should catch most stuff. Won't find the variables in the tag though.

    After that do a toLower on the group found and send it back in. Probably an easier way to do this via the DOM though.

    Thanks for that. I should have said it was XHTML, which is case sensitive.

    I also hope to use this with an XML feed I am working with (the feed elements are in camel case, but the elements I'm matching in our DB are always lowercase and neither can be changed at source).


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes




  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    Thanks again Hobbes - it's a bit of a tricky one as the feed I am working off is completely configurable, as are the fields I am trying to match (they are added through a web-based UI). I think a C# component in the middle is the best solution as I can just iterate through the nodes and rename them.


  • Moderators, Science, Health & Environment Moderators Posts: 9,220 Mod ✭✭✭✭mewso


    I think this will do it for you:-
    [SIZE=2]mystring = String(mystring).replace(/<(\/?[A-Z][^>]*)>/g, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]function [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](match) {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] match.toLowerCase();
    }
    );
    
    [/SIZE]


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    Works a treat, thanks very much!


  • Moderators, Science, Health & Environment Moderators Posts: 9,220 Mod ✭✭✭✭mewso


    Years of dealing with word crap.


Advertisement