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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:27:33+00:00 2026-05-28T13:27:33+00:00

Consider the following example: <html> <body> <script type=text/javascript> var str=filename.jpg; var pattOne = new

  • 0

Consider the following example:

<html>
<body>

<script type="text/javascript">

var str="filename.jpg";

var pattOne = new RegExp('\.[^\.]*$');
var pattTwo = new RegExp('(\.[^\.]*$)');
var pattThree = new RegExp('(\.[^\.]*$)', 'g');

document.write(str.match(pattOne));
document.write('<br>');
document.write(str.match(pattTwo));
document.write('<br>');
document.write(str.match(pattThree));

</script>
</body>
</html>

Here is the result:

.jpg
.jpg,.jpg
.jpg

I expect this result:

.jpg
.jpg
.jpg

Why placing parenthesis around the regular expression changes the result? Why using ‘g’ modifier changes again the result?

  • 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-28T13:27:34+00:00Added an answer on May 28, 2026 at 1:27 pm

    From String.prototype.match [MDN]:

    If the regular expression does not include the g flag, returns the same result as regexp.exec(string).

    Where the RegExp.prototype.exec documentation [MDN] says:

    The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.

    So as you introduced a capture group in the second expression, the first element is the whole match and the second contains the content of the capture group, which, in your example, is the whole match as well.
    The first expression does not have a capture group, so you only get back the match.

    Back to the match documentation:

    If the regular expression includes the g flag, the method returns an Array containing all matches. If there were no matches, the method returns null.

    With the g modifier, only matches are returned, but not the content of capture groups. In your string there is only one match.

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

Sidebar

Related Questions

Consider the following example: ( demo here ) HTML: <input type=text /> <div>Click here
Consider the following example code: http://code.google.com/apis/maps/documentation/javascript/examples/streetview-simple.html I can do scrollwheel: false on a mapOptions
Consider the following HTML: (Written for Google Chrome) <html> <head> <script language=javascript> function AddToIFrame(){
Consider the following example : HTML: <div class=wrapper> <div class=left>Some text here</div><div class=right>Hello Stack
Consider the following example: int size = 10, *kk = new int[size]; for (int
Consider the following example: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd> <html xmlns=http://www.w3.org/1999/xhtml
Please consider the following HTML: <td> Some Text <table>.....</table> </td> I need to manipulate
Consider the following example: ( live demo here ) HTML: <label>Name</label> CSS: label {
Consider the following example from APUE book: #include <errno.h> #include <sys/socket.h> int initserver(int type,
Consider the following example: ( live demo here ) HTML: <div id=outer_wrapper> <div class=wrapper>

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.