Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

JavaScript/REGEX: Replace the 3rd match

Posted on 2016-09-05
9
39 Views
Last Modified: 2016-09-10
Using JavaScript and Regular Expressions, how can I replace the third match?

For example, I want JavaScript to return this:
hello[11][22][X][44][55]'
var str = 'hello[11][22][33][44][55]';
alert( str.replace(/(\[[0-9]+\]){3}/, '[X]')  );

Open in new window

0
Comment
Question by:skij
  • 4
  • 3
  • 2
9 Comments
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41784959
You can try
var r = str.replace(/(\[\d+\]\[\d+\])(\[\d+\])/, '$1[X]') ;

Open in new window

0
 
LVL 22

Assisted Solution

by:Kim Walker
Kim Walker earned 200 total points
ID: 41784962
You're going to have to be a little more specific. Search for the first two bracketed numbers literally and include them in the replace with a callback.
var str = 'hello[11][22][33][44][55]';
alert( str.replace(/(\[[0-9]+\]\[[0-9]+\])\[[0-9]+\]/, '$1[X]')  );

Open in new window

0
 
LVL 10

Author Comment

by:skij
ID: 41784980
Is there a way to accommodate the possibility of text between brackets in the strong?

In the example below, [33] should be replaced with [X] in both scenarios.
var str = 'hello[11][22][33][44][55]';
var str = 'hello[11][world][22][example7][33][value][44][test][55]';

Open in new window

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:Kim Walker
ID: 41785010
Now [33] is not the third bracketed set of characters. It is the fifth. How do you know to replace the fifth one in this case? Or are you always replacing [33] precisely?
0
 
LVL 10

Author Comment

by:skij
ID: 41785048
I want to replace the third group of bracketed numbers.  So [123] should count but [xyz] should not.  I only want the third group of bracketed numbers.  Thanks!
0
 
LVL 55

Assisted Solution

by:Julian Hansen
Julian Hansen earned 300 total points
ID: 41785113
Try this
str.replace(/(\[\d+\])(.*?)\[\d+\](.*?)(\[\d+\])/, '$1$2$3$4[X]') ;

Open in new window

0
 
LVL 10

Accepted Solution

by:
skij earned 0 total points
ID: 41785219
Thank you.  Julian, I made a slight adjustment because your code wasn't quite producing the expected results.  

This works:
str.replace(/(\[\d+\])(.*?)(\[\d+\])(.*?)\[\d+\]/, '$1$2$3$4[X]')

Open in new window

0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41785228
Yes, I see I left out the 3rd set of (  )
0
 
LVL 10

Author Closing Comment

by:skij
ID: 41792502
It works!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question