Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Javascript RegEx Help

  1. #1

    Default Javascript RegEx Help

    Trying to remove all block elements from a string containing HTML. Below doesn't work properly though.

    Code:
    for(var i = 0; i < blockElems.length; i++){
                  var pattern = '(<' + blockElems[i] + '[^>]*>|<\/' + blockElems[i] + '>)';
                  var re = new RegExp(pattern, 'gi');
                  selectedElem.replace(re, '');
                }
    Anyone see anything wrong with my RegEx pattern or something?
    Browse MOHAA Servers Post GameSpy Era

    VISIT MOHREBORN.COM FOR LATEST INFORMATION



    Medal of Honor: Game Server Browser Fixer - Patches your MOHAA, MOHSH, and MOHBT game binaries to allow you to retrieve a list of game servers within the multi-player menu in-game even after GameSpy ceases operation!

    Medal of Honor: Query Launcher - Find, browse, organize, join, get your ping, and get more information regarding all Medal of Honor (AA, SH, & BT) servers from your PC at any time!
    Medal of Honor: Web Server Master List - Find and browse all Medal of Honor servers online using your browser!
    Add your Medal of Honor Server to the Master List
    YouTube Video for Medal of Honor: Query Launcher and MOHAASERVERS.TK!



    MOHAA Mods and Utilities
    OwN-3m-All's Mods
    Make Me Stock - A program that allows you to easily move-in and move-out non-stock mods and other files at the click of a button. Automates adding / removing mods without having to copy / move files manually.



    Quality Game Servers

    Rent dedicated Dallas Texas, Kansas City, Las Vegas Nevada, Chicago, Pennsylvania, and Sofia Bulgaria MOHAA and other game servers from We Be HostiN starting at $10 a month.


  2. #2
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    I never was a fan of java\js, but maybe try defining the variable globally or outside of the for() loop.
    also, I'm trying to understand exactly what the pattern variable is holding.. Too many symbolic characters in there for me to follow haha.

    you're setting a pattern that's something like this?
    Example: <something*> or <something>

    EDIT: Saw this: http://forums.devshed.com/javascript...ng-943695.html

    Made more sense to me. Let me take another look at this.


    EDIT2:

    Try this
    Code:
    var pattern = '/(<' + blockElems[i] + '([^>]+)>/ig';
    or
    var pattern = /(<([^>]+)>)/ig;
    on a side note, are you able to use jQuery?

    Here are some links that could be helpful:
    http://stackoverflow.com/questions/1...ipt-with-regex
    http://stackoverflow.com/questions/8...ext-javascript
    http://stackoverflow.com/questions/5...gs-from-string
    http://css-tricks.com/snippets/javas...in-javascript/
    http://geekswithblogs.net/aghausman/...avascript.aspx
    http://robertnyman.com/roblab/javasc...emove-tags.htm
    http://www.mximize.com/how-to-strip-...from-a-string-
    http://www.aspforums.net/Threads/201...ression-Regex/ <-- shows demo

  3. #3
    Banned
    Join Date
    May 2010
    Location
    fuck off?
    Posts
    1,145

    Default

    just google JS "strip HTML" this has probs been done a million times over :\

  4. #4

    Default

    Quote Originally Posted by Elgan View Post
    just google JS "strip HTML" this has probs been done a million times over :\
    No, I don't want to remove "All" HTML. I only want to remove block elements. Inline html elements must stay. This is because I'm working with selected code from the CKEditor and formatting them into other block elements that contain specific styles.

    Actually, I think both RegEx patterns would work, but I believe my mistake is as simple as this:

    Code:
    selectedElem = selectedElem.replace(re, '');
    Though, * should be used in the pattern as + is looking for one or more characters, but if you have <p> there are no characters after the element that are not '>'

    I haven't tested what I posted above. I won't be able to until I'm back from work :\

    I'm glad I'm not having to write a parser for this... I think this is my greatest weakness in programming... unless it's easy like it is for the mohaaconfiggenerator application I wrote...
    Browse MOHAA Servers Post GameSpy Era

    VISIT MOHREBORN.COM FOR LATEST INFORMATION



    Medal of Honor: Game Server Browser Fixer - Patches your MOHAA, MOHSH, and MOHBT game binaries to allow you to retrieve a list of game servers within the multi-player menu in-game even after GameSpy ceases operation!

    Medal of Honor: Query Launcher - Find, browse, organize, join, get your ping, and get more information regarding all Medal of Honor (AA, SH, & BT) servers from your PC at any time!
    Medal of Honor: Web Server Master List - Find and browse all Medal of Honor servers online using your browser!
    Add your Medal of Honor Server to the Master List
    YouTube Video for Medal of Honor: Query Launcher and MOHAASERVERS.TK!



    MOHAA Mods and Utilities
    OwN-3m-All's Mods
    Make Me Stock - A program that allows you to easily move-in and move-out non-stock mods and other files at the click of a button. Automates adding / removing mods without having to copy / move files manually.



    Quality Game Servers

    Rent dedicated Dallas Texas, Kansas City, Las Vegas Nevada, Chicago, Pennsylvania, and Sofia Bulgaria MOHAA and other game servers from We Be HostiN starting at $10 a month.


  5. #5
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    if you want to remove a certain amount of elements.. do this..

    take one of the working examples that I posted above, then in the function, have a blacklist of some sort that it checks. For example
    if(<div> || <p> || <strong>)
    {
    do this
    }
    else if(another condition)
    {
    do this
    }
    else
    {
    do this
    }

    JS is difficult because it doesn't have any kind of syntax\error checking. It's mostly trial and error.

    If I compare it to a compiler like c or c++ if you miss something as simple as a bracket or comma it points to the line. It really helps debug code.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    fuck off?
    Posts
    1,145

    Default

    ah oki

    I didnt really read it :P I find it always good to just write the regex in a texteditor that supports it and test it there, then I know it works and can translate...as long as they are the same or similar version

  7. #7
    Über Prodigy & Developer Razo[R]apiD's Avatar
    Join Date
    May 2010
    Location
    Poland, Lublin
    Posts
    3,257

    Default

    regexp's are something I've never really learned.. (unbeliveable isn't it? ;p)

    If you don't need to use regexp, you can find:

    <blockelem or </blockelem>

    then read until you find closing bracket >

    find it and replace

  8. #8
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    regexp's are something I've never really learned.. (unbeliveable isn't it? ;p)


    First the STD library in c, now regexp in js.. hmmm.. RR you're slipping. LOL :-P

  9. #9
    Über Prodigy & Developer Razo[R]apiD's Avatar
    Join Date
    May 2010
    Location
    Poland, Lublin
    Posts
    3,257

    Default

    In C++ to be exact ;p

  10. #10

    Default

    I was right.

    Code:
    selectedElem.replace(re, '');
    To:

    Code:
    selectedElem = selectedElem.replace(re, '');
    Stupid mistake.... fail. Oh well. Of course you must reassign the replaced text back into the variable.

    Works like it is supposed to! Thanks guys!
    Browse MOHAA Servers Post GameSpy Era

    VISIT MOHREBORN.COM FOR LATEST INFORMATION



    Medal of Honor: Game Server Browser Fixer - Patches your MOHAA, MOHSH, and MOHBT game binaries to allow you to retrieve a list of game servers within the multi-player menu in-game even after GameSpy ceases operation!

    Medal of Honor: Query Launcher - Find, browse, organize, join, get your ping, and get more information regarding all Medal of Honor (AA, SH, & BT) servers from your PC at any time!
    Medal of Honor: Web Server Master List - Find and browse all Medal of Honor servers online using your browser!
    Add your Medal of Honor Server to the Master List
    YouTube Video for Medal of Honor: Query Launcher and MOHAASERVERS.TK!



    MOHAA Mods and Utilities
    OwN-3m-All's Mods
    Make Me Stock - A program that allows you to easily move-in and move-out non-stock mods and other files at the click of a button. Automates adding / removing mods without having to copy / move files manually.



    Quality Game Servers

    Rent dedicated Dallas Texas, Kansas City, Las Vegas Nevada, Chicago, Pennsylvania, and Sofia Bulgaria MOHAA and other game servers from We Be HostiN starting at $10 a month.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •