Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8716609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:07:28+00:00 2026-06-13T06:07:28+00:00

I have user’s firstname and lastname in one string, with space between e.g. John

  • 0

I have user’s firstname and lastname in one string, with space between
e.g.
John Doe
Peter Smithon

And now I want convert this string to two string – firstname and lastname
John Doe -> first = John, last = Doe
John -> first = John, last = “”
[space]Doe -> first = “”, last = Doe.

I am using next code

var fullname = "john Doe"
var last = fullname.replace(/^.*\s/, "").toUpperCase().trim(); // john
var first = fullname.replace(/\s.*$/, "").toUpperCase().trim(); // Doe  

and this works well for two-word fullname. But if fullname has one word, then I have problem

var fullname = "john"  
var last = fullname.replace(/^.*\s/, "").toUpperCase().trim(); // john
var first = fullname.replace(/\s.*$/, "").toUpperCase().trim(); // john  

http://jsfiddle.net/YyCKx/
any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:07:30+00:00Added an answer on June 13, 2026 at 6:07 am

    Use split + shift methods.

    var parts = "Thomas Mann".split(" "),
        first = parts.shift(),
        last = parts.shift() || "";
    

    So in case of single word name it will give you expected result:

    last = "";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have User and Client, both of which have one Address. I want to
i have user control, which i render on several views. i want to show
Following scenario: I have user object: ... @Column(name = TITLE) private String title; @NotNull
I have user input in a script - $_POST['money'] and I want to confirm
I have user HeaderedItemsControl to display different UserControl in my application. Now I declared
I have user submitted tags that can be any type of (valid) UTF-8 string.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have user document collection like this: User { id:001 name:John, age:30, friends:[userId1,userId2,userId3....] }
One user may have just 1 item or none. (1-1..0 relationship) I'm trying to
I have user with name, location, created_at as important fields in table. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.