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 9016405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:59:14+00:00 2026-06-16T03:59:14+00:00

I have a string that looks like this: Doe, John, A (lastname, firstname, middle

  • 0

I have a string that looks like this: “Doe, John, A” (lastname, firstname, middle initial).

I’m trying to write a regular expression that converts the string into “Doe*John*A”.

However, I have to take into account all spaces for this string so “Doe , John , A” would still convert into “Doe*John*A”.

ALSO, the string “Doe John A” should convert into “Doe*John*A”.

I started writing this, but I think I’m stuck on the spaces & the possibility of the user not supplying the commas.

Here’s what I have:

var myString = "John, Doe, A";
var myOtherString = "John  Doe   A";


var myFunction = function (aString) {
        aString = aString.replace(", ", "*");
        aString = aString.replace(", ", "*");

return aString;

};

These should both return "Doe*John*A".

I think I’m repeating myself too much in this function. I’m also not taking into account the possibility that no commas will be provided.

Is there a better way to do this?

  • 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-16T03:59:15+00:00Added an answer on June 16, 2026 at 3:59 am

    Yes, there is. Use the replace function with a regex instead. That has a few advantages. Firstly, you don’t have to call it twice anymore. Secondly it’s really easy to account for an arbitrary amount of spaces and an optional comma:

    aString = aString.replace(/[ ]*,[ ]*|[ ]+/g, '*');
    

    Note that the square brackets around the spaces are optional, but I find they make the space characters more easily readable. If you want to allow/remove any kind of whitespace there (tabs and line breaks, too), use \s instead:

    aString = aString.replace(/\s*,\s*|\s+,/g, '*');
    

    Note that in both cases we cannot simply make the comma optional, because that would allow zero-width matches, which would introduce a * at every single position in the string. (Thanks to CruorVult for pointing this out)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that looks like this: -2.2,1.1-6.9,2.3-12.8,2.3 And I want to write
I have strings that looks like this: John Miller-Doe - Name: jdoe Jane Smith
I have a String that looks like this String = Förpackning Flaska (375 ml)
I have a string that looks like this: <name>-<gender>-<age>.jpg I want to be very
I have a string that looks like this: 9/1/2009. I want to convert it
i have a string that looks like this: sodjfoisdfsdf sdofij sodiosifosf fsdi a123 sdfoi
Problem: I have a string that looks like this: [1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18] Question: How can you
I have a huge string that looks like this: Text Text Text Text Text
I have a JSON string that looks like this: { package1: { type: envelope,
I have been passed a date string that looks like this: Thu%20Mar%2011%202010%2015%3A09%3A11%20GMT%2B0000%20(BST) 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.