Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. problem regarding the white spaces and trimming in the regular expression using the replace function [modified]

problem regarding the white spaces and trimming in the regular expression using the replace function [modified]

Scheduled Pinned Locked Moved ASP.NET
regexhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sonia Gupta
    wrote on last edited by
    #1

    Following is the expression , that represents the trimming of the string , which has the white space. function LTrim( value ) { var re = /\s*((\S+\s*)*)/; return value.replace(re, "$1"); } function RTrim( value ) { var re = /((\s*\S+)*)\s*/; return value.replace(re, "$1"); } function trim( value ) { return LTrim(RTrim(value)); } and am unable to understand the meaning of the expression /\s*((\S+\s*)*)/ Will somebody please make me understand

    Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

    modified on Tuesday, January 01, 2008 6:38:17 AM

    U 1 Reply Last reply
    0
    • S Sonia Gupta

      Following is the expression , that represents the trimming of the string , which has the white space. function LTrim( value ) { var re = /\s*((\S+\s*)*)/; return value.replace(re, "$1"); } function RTrim( value ) { var re = /((\s*\S+)*)\s*/; return value.replace(re, "$1"); } function trim( value ) { return LTrim(RTrim(value)); } and am unable to understand the meaning of the expression /\s*((\S+\s*)*)/ Will somebody please make me understand

      Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

      modified on Tuesday, January 01, 2008 6:38:17 AM

      U Offline
      U Offline
      UsmanMunier
      wrote on last edited by
      #2

      the first \s* leaves any number of blank spaces the inner one (\S+\s) matches one more non space characters and a space.. for example.. "asm " or "a " etc. And the outer ((\S+\s)*) matches any number of these words.. in short, this regex breaks a sentence into words.. for ex. " My Name Is ABC XYZ" can be extracted as "My_", "Name_", "Is_", "ABC_" XYZ isn't captured because it does not have a trailing space and so doesn't match (\S+\s) Regards,

      Usman Munier Xavor Corporation Pakistan

      S 1 Reply Last reply
      0
      • U UsmanMunier

        the first \s* leaves any number of blank spaces the inner one (\S+\s) matches one more non space characters and a space.. for example.. "asm " or "a " etc. And the outer ((\S+\s)*) matches any number of these words.. in short, this regex breaks a sentence into words.. for ex. " My Name Is ABC XYZ" can be extracted as "My_", "Name_", "Is_", "ABC_" XYZ isn't captured because it does not have a trailing space and so doesn't match (\S+\s) Regards,

        Usman Munier Xavor Corporation Pakistan

        S Offline
        S Offline
        Sonia Gupta
        wrote on last edited by
        #3

        i was trying to figure out the solution of trimming. i reached the following expression function ltrim(s) { var temp = s; alert('hi'); var obj = /^(\s*)([\W\w]*)/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } return temp } function rtrim() { var temp = s; alert('hi'); var obj = /^([\W\w]*)(\s*)$/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } return temp } function trim(s) { return rtrim(ltrim(s)); } I think there is missing something. One more thing , What is the difference between $1 and $2. Please help

        Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

        modified on Tuesday, January 01, 2008 8:07:19 AM

        U 1 Reply Last reply
        0
        • S Sonia Gupta

          i was trying to figure out the solution of trimming. i reached the following expression function ltrim(s) { var temp = s; alert('hi'); var obj = /^(\s*)([\W\w]*)/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } return temp } function rtrim() { var temp = s; alert('hi'); var obj = /^([\W\w]*)(\s*)$/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } return temp } function trim(s) { return rtrim(ltrim(s)); } I think there is missing something. One more thing , What is the difference between $1 and $2. Please help

          Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

          modified on Tuesday, January 01, 2008 8:07:19 AM

          U Offline
          U Offline
          UsmanMunier
          wrote on last edited by
          #4

          i have a better solution which i have verified as well on my many application try this. i hope this will solve your problem function TrimWhiteSpaces(s) { s=s.replace(/^\s+|\s+$/g, ""); var myArray=s.split(" "); var outPut=""; for (var i=0;i<myArray.length;i++) { if(myArray[i]!="") { outPut=outPut +" "+ myArray[i]; } } s=outPut; return s; } wish you all the best

          Usman Munier Xavor Corporation Pakistan

          S 1 Reply Last reply
          0
          • U UsmanMunier

            i have a better solution which i have verified as well on my many application try this. i hope this will solve your problem function TrimWhiteSpaces(s) { s=s.replace(/^\s+|\s+$/g, ""); var myArray=s.split(" "); var outPut=""; for (var i=0;i<myArray.length;i++) { if(myArray[i]!="") { outPut=outPut +" "+ myArray[i]; } } s=outPut; return s; } wish you all the best

            Usman Munier Xavor Corporation Pakistan

            S Offline
            S Offline
            Sonia Gupta
            wrote on last edited by
            #5

            What's the problem in the following chunk of code (i don;t have any problem regarding Left Trim ) function rtrim(s) { var temp = s; var obj = /([\W\w]*)(\s*)$/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } return temp }

            Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

            modified on Wednesday, January 02, 2008 12:14:43 AM

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups