Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2005
    Thanked 0 Times in 0 Posts

    Regex breaks down when it encounters a degree symbol º or ampersand &

    Hi there. I have a javascript code that packages an array as a string and sends it to a php mailer, which then separates the string into an array for mailing using a preg_split regular expression that searches for a comma followed by an html tag.

    $myWrongArray = preg_split("/,(?=<)/",$myVar); // lookahead splits the string when the comma is before a tag (the opening bracket < )
    //The look ahead is (?=<), which interrogates the next character and if it is the angle bracket, <, the preg_split function operates.

    Everything works great EXCEPT when the string contains a degree sign º or an &, in which case the array is returned up to (but not including) the º or & and then stops.

    I can’t for the life of me understand it. Any ideas?

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Behind the Wall
    Thanked 609 Times in 602 Posts
    Can you post an example where that happens?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. #3
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Keene, NH
    Thanked 535 Times in 521 Posts
    I think @Dormilich is right in that we need to see a sample string where it's screwing up... but... well...

    That it appears you're trying to use regex on markup from JavaScript, means you are likely handling markup in JavaScript... where markup USUALLY has ZERO business being in the first place... at least if this is client side. Even if server-side, you'd be building not deconstructing so I'm curious as to the usage case.

    What are you working on where the bad practice of trying to manipulate markup from JavaScript is even a thing? Is this something you could parse down in a dummy DIV to a DOM node tree, then handle by DOM walking checking for comma's at the end of textNodes?
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture


Tags for this Thread

Posting Permissions

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