"match" issues with non-alphanumberic characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phub11
    New Member
    • Feb 2008
    • 127

    #1

    "match" issues with non-alphanumberic characters

    Hi all,

    I am using "match" to see if elements in one array match elements in another array.

    My first array is "1.1,1.2,2. 1" so I convert it to a string. Next, I have a loop that cycles through the second array, converts the current element of the second array to a string, and then uses "match" to see if this occurs in the string corresponding to the first array.

    "match" seems to get confused if I have an element such as "2.2" in the second array, as it thinks it matches "....2,2... ." in the string corresponding to the first array.

    Any ideas on how to fix this?

    Thanks in advance
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Why bother with strings when you have the values and can match directly?

    When using match, the string is converted to a regular expression where the dot character can match any character including comma.

    Comment

    • phub11
      New Member
      • Feb 2008
      • 127

      #3
      Thanks for the reply.

      I thought it would be quicker using "match" with string than having a subloop (for each element within array 2) within a loop (for each element within array 1).

      Am I misunderstandin g your suggestion?

      Thanks!

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Fair enough, though I'm not sure about the exact performance differences.

        For your problem, replace the "." with "\.", so 2.2 becomes 2\.2, so that the dot is matched and not any character.

        Comment

        • phub11
          New Member
          • Feb 2008
          • 127

          #5
          Thanks for the reply.

          In the end, my work around was to change "string.split(' ,')" to "string.split(' X')".

          EDIT: Oops, that doesn't work!

          Thanks again!

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Why would you be using split (producing an array) when you already had one in the first place?!

            As I said, use the backslash \ to escape the dot.

            Comment

            • phub11
              New Member
              • Feb 2008
              • 127

              #7
              Sorry, hadn't looked at it until last night and forgot where I was!

              The problem with your suggestion in that I'd have to edit a lot of other functions (each element is called from a cell ID in a table).

              The correct work around is: string1 = string.replace(/,/g, "XXX")

              Works for me so far.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                That would work, but be careful about the fact that it's a fix because the dot is still matching any character. It doesn't match 2,2 because that's now become 2XXX2.

                Comment

                Working...