Solved

JavaScript/REGEX: Replace the 3rd match

Posted on 2016-09-05
9
48 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
[X]
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
  • 4
  • 3
  • 2
9 Comments
 
LVL 58

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 58

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 58

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

636 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