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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:38:49+00:00 2026-05-30T15:38:49+00:00

Why does javascript replace string function do this? aaa\nbbb\nccc.replace(/.*/gm, .) // result = ..\n..\n..

  • 0

Why does javascript replace string function do this?

"aaa\nbbb\nccc".replace(/.*/gm, ".")
// result = "..\n..\n.." but expected was: ".\n.\n."

"aaa\nbbb\nccc".replace(/^.*/gm, ".")
// result = ".\n.\n." -> OK!!!

"aaa\nbbb\nccc".replace(/.*$/gm, ".")
// result = "..\n..\n.." but expected was: ".\n.\n."

What I am doing wrong?

  • 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-05-30T15:38:50+00:00Added an answer on May 30, 2026 at 3:38 pm

    Let me address those in reverse order:

    What I am doing wrong?

    You want to use +, not *. * means zero or more matches, which makes no sense here. + means one or more matches. So:

    "aaa\nbbb\nccc".replace(/.+/g, ".")
    // ".\n.\n."
    

    Also note that if you’re not using ^ or $ (your first example), you don’t need the m modifier (but that wasn’t the problem with what you were doing). And you don’t need ^ or $ because . doesn’t match newlines (something I didn’t know prior to answering this question).

    Why does javascript replace string function do this?

    I have no Earthly idea and hope someone else does.

    Again, by using * you’re saying zero or more matches. So it matches all of the relevant characters, replacing them with the first dot; then it matches zero characters, replacing them with one dot. Result: Two dots.

    Proof:

    Live copy | Live source

    "aaa\nbbb\nccc".replace(/.*/g, function(m) {
        console.log("m = '" + m + "'");
    });
    

    Outputs:

    m = 'aaa'
    m = ''
    m = 'bbb'
    m = ''
    m = 'ccc'
    m = ''
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just saw this piece of javascript code at Facebook: javascript:eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return
Does the JavaScript regex standard support forcing case when doing a search/replace? I am
JavaScript does funky automatic conversions with objects: var o = {toString: function() {return 40;
Why does this javascript return 108 instead of 2008? it gets the day and
Why does the JavaScript function encodeURIComponent encode spaces to the hex Unicode value %20
Which algorithm does the JavaScript Array#sort() function use? I understand that it can take
This Javascript that I found here adds a method parseURL() to the string object
I have a Javascript function that is passed a string. The string that it
I faced this strange situation where foreach like construct of javascript does not work
I have a working reg expression that does a replace function based on the

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.