Solved

JavaScript/REGEX: Replace the 3rd match

Posted on 2016-09-05
9
25 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 51

Expert Comment

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

Open in new window

0
 
LVL 21

Assisted Solution

by:Kim Walker
Kim Walker earned 200 total points
Comment Utility
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
Comment Utility
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
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 10

Author Comment

by:skij
Comment Utility
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 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 300 total points
Comment Utility
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
Comment Utility
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 51

Expert Comment

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

Author Closing Comment

by:skij
Comment Utility
It works!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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 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)

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now