Solved

JavaScript/REGEX: Replace the 3rd match

Posted on 2016-09-05
9
43 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 56

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 56

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 56

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

730 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