0

I have got a text area and a function that splits the pasted content based on the spaces between content elements and turns them into labels one by one:

Say I have the following content to be pasted:

1234,john smith,john@test.com 4312,jack gold,jack@yahoo.com 5678,Brian,brian@gmail.com

and obviously I use

$('#testArea').on("paste", ".maininput", function (event) {
     var text = $(element).val();
     var contentArray = text.split(" ");
}

The result should be 3 labels with the following format (users mobile number,full name, email)

But because of the fact that there are spaces between firstname and lastname I am not able to get the right result.

What I am trying to achieve is sort of escaping the spaces when its between first and last name.

has anyone got any idea how to do it?

4

5 回答 5

4

Don't split on spaces. Instead, scan for what you want:

var s = "1234,john smith,john@test.com 4312,jack gold,jack@yahoo.com 5678,Brian,brian@gmail.com"
var lines = s.match(/\S[^,]+,[^,]+,[^ ]+/g)
for (var i=lines.length;i--;){
  console.log(lines[i].split(','));
}
// ["5678", "Brian", "brian@gmail.com"]
// ["4312", "jack gold", "jack@yahoo.com"]
// ["1234", "john smith", "john@test.com"]

That regex says:

  • Find something other than whitespace
  • Followed by one or more things that are not a comma
    • Followed by a comma
  • Followed by one or more things that are not a comma
    • Followed by a comma
  • Followed by one or more things that are not a space
于 2013-07-12T05:30:03.417 回答
1

Better to use a regular expression to match the pattern.

var str = "1234,john smith,john@test.com 4312,jack gold,jack@yahoo.com 5678,Brian,brian@gmail.co";
var matchGroups = str.match(/([^,]*,[^,]*,[^ ]*)/g);  //look for pattern "XXX,XXX,XXX" followed by whitespace or end of line
console.log(matchGroups);

//Now work with the sections
for( var i=0;i<matchGroups.length;i++){
    var parts = matchGroups[i].split(",");  //split it into your parts on commas
    console.log(parts);
}

JSFiddle

于 2013-07-12T05:34:02.200 回答
0

For example, replace <space><digit> with |<digit> and then split on |:

text.replace(/ (\d)/g, "|$1").split("|")

Example:

"1234,john smith,john@test.com 4312,jack gold,jack@yahoo.com 5678,Brian,brian@gmail.com".replace(/ (\d)/g, "|$1").split("|")

["1234,john smith,john@test.com",
 "4312,jack gold,jack@yahoo.com",
 "5678,Brian,brian@gmail.com"]
于 2013-07-12T05:29:29.917 回答
0

you can run a for loop to check the next character of space, and based on it you can replace space with &nbsp; or leave it as it is. I mean if the next character is a number you can simply leave the space as it is and if it is a letter change space to &nbsp;

于 2013-07-12T05:32:27.283 回答
0
jQuery( window ).load(function() {
            jQuery("#FullNametest").change(function(){
                var temp = jQuery(this).val();
                var fullname = temp.split(" ");
                var firsname='';
                var middlename='';
                var lastname = '';
                firstname=fullname[0];
                lastname=fullname[fullname.length-1];

                for(var i=1; i < fullname.length-1; i++) 
                {
                   middlename =  middlename +" "+ fullname[i];
                }
                jQuery('#FirstName').val(firstname);
                jQuery('#middlename').val(middlename);
                jQuery('#LastName').val(lastname);
            });
        });
于 2014-06-03T11:01:38.740 回答