Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Regular Expressions + JavaScript

  • 28-05-2008 1:57pm
    #1
    Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭


    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,263 ✭✭✭✭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,263 ✭✭✭✭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,035 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,263 ✭✭✭✭Eoin


    Works a treat, thanks very much!


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


    Years of dealing with word crap.


Advertisement