Searching string with regular expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amollokhande1
    New Member
    • Feb 2008
    • 33

    Searching string with regular expression

    Hi,

    I have following string data

    Amol\'Lokhande. Now I want to search \' from the string.

    I have used following code

    Code:
    sValue="Amol\'Lokhande"
    var objRegex =new RegExp("\\\\'","g");
    sValue.search(objRegex)
    but still it is not working. Can anybody help me to resolve this problem.

    What will be the regular expression to search \' string

    Regards
    Amol Lokhande
    Last edited by gits; Jan 22 '09, 05:21 PM. Reason: use code tags instead of bold tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    this should work:

    [CODE=javascript]var sValue = "Amol\'Lokhande ";

    var objRegex = new RegExp("\'", "g");

    alert(objRegex. test(sValue));
    [/CODE]
    kind regards

    Comment

    • amollokhande1
      New Member
      • Feb 2008
      • 33

      #3
      Hi gits

      It is not working. If return ture even if we remove \ from the string Amol'Lokhande

      Regards
      Amol Lokhande

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        for JavaScript the "\'" == "'" ... so the backslash is an escape-char already in the string ... basicly you create a new string "Amol'Lokha nde" in your code ... i've to look deeper into that issue ... don't know a solution other then have the \ escaped already before you try to create a JavaScript object from it ... in case you want to preserve it.

        kind regards

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          wouldn't it be easier to use a string search? like
          Code:
          if (sValue.indexOf("\'") != -1) { ... }

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            that would even match ' or wouldn't it? ... may be i miss something?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              the problem is the original string. try alert(sValue); and you'll see.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                yep ... therefore i just suggested to escape that before using it in JavaScript when that chars need to be preserved ... but may be its just misleading? may be we should know whatfor that match should be used ... currently i don't see any problem because the backslash is just dropped from the string by the string-construction itself?

                kind regards

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  Originally posted by gits
                  ... because the backslash is just dropped from the string by the string-construction itself?
                  searching for the backslash was pointless from the beginning – that's what the alert taught me.

                  PS: I should have known, I was trying to find "März" in one of my scripts – and failed

                  Comment

                  Working...