what is the reg exp to do this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    what is the reg exp to do this?

    Hi,

    I want to create a regular expression that will take a single input
    name field and save the first and last names to two separate js vars.
    The criteria for determining what is the separation would be the first
    occurrence of a space (ascii code = 32). However, note that a user
    may only enter one word or nothing at all. If the DOM id for the
    field I want to parse is "nameField" , how would I do this?

    Thanks, - Dave

  • Randy Webb

    #2
    Re: what is the reg exp to do this?

    laredotornado@z ipmail.com said the following on 5/23/2007 5:03 PM:
    Hi,
    >
    I want to create a regular expression that will take a single input
    name field and save the first and last names to two separate js vars.
    The criteria for determining what is the separation would be the first
    occurrence of a space (ascii code = 32). However, note that a user
    may only enter one word or nothing at all. If the DOM id for the
    field I want to parse is "nameField" , how would I do this?
    Why are you making it harder than it has to be?

    var theNames = refToFormElemen t.split(' ');

    If theNames has a length of 1, then there was no space.
    If theNames has a length of 2, then there was a space.
    If theNames has a length 2, then there was more than one space.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

    Comment

    • scripts.contact

      #3
      Re: what is the reg exp to do this?

      On May 23, 3:03 pm, "laredotorn...@ zipmail.com"
      <laredotorn...@ zipmail.comwrot e:
      Hi,
      >
      I want to create a regular expression that will take a single input
      name field and save the first and last names to two separate js vars.
      The criteria for determining what is the separation would be the first
      occurrence of a space (ascii code = 32). However, note that a user
      may only enter one word or nothing at all. If the DOM id for the
      field I want to parse is "nameField" , how would I do this?
      >
      Thanks, - Dave
      var fullname=docume nt.getElementBy Id("nameField") ;
      var firstSpace=full name.indexOf(' ');
      WScript.Echo(
      "First Name:"+fullname .substr(0,first Space)
      +"\nLast Name:"+fullname .substr(firstSp ace+1)
      )

      Comment

      • scripts.contact

        #4
        Re: what is the reg exp to do this?

        On May 23, 4:28 pm, "scripts.contac t" <scripts.cont.. .@gmail.com>
        wrote:
        var fullname=docume nt.getElementBy Id("nameField") ;
        var firstSpace=full name.indexOf(' ');
        WScript.Echo(
        "First Name:"+fullname .substr(0,first Space)
        +"\nLast Name:"+fullname .substr(firstSp ace+1)
        )
        alert(
        "First Name:"+fullname .substr(0,first Space)
        +"\nLast Name:"+fullname .substr(firstSp ace+1)
        )

        Comment

        • Michael White

          #5
          Re: what is the reg exp to do this?

          laredotornado@z ipmail.com wrote:
          Hi,
          >
          I want to create a regular expression that will take a single input
          name field and save the first and last names to two separate js vars.
          The criteria for determining what is the separation would be the first
          occurrence of a space (ascii code = 32). However, note that a user
          may only enter one word or nothing at all. If the DOM id for the
          field I want to parse is "nameField" , how would I do this?
          >
          Thanks, - Dave
          >
          A= String.split(/\s+/);AA=A.length;
          firstName=AA<2? "":A[0];
          lastName=AA==1? A[0]:AA>1?A.slice(1 ).join(" "):"Empty";
          Mick


          Comment

          Working...