Solved

JavaScript / jQuery RegEx Replacement

Posted on 2013-06-24
3
431 Views
Last Modified: 2013-06-25
Hello,

I'm trying to use jQuery to replace some extraneous data that's hard-coded into a CMS I'm using. The text contains anchor links, but is displayed as such:

PANASONIC > Panasonic Upright Vacuums > MC-V5241-02
PANASONIC > Panasonic Upright Vacuums > MC-UL675-00
PANASONIC > Panasonic Upright Vacuums > MC-UL671-00

Open in new window


I'm trying to cut it down to display like this, but with the proper link attached to the last item:

>> MC-V5241-02
>> MC-UL675-00
>> MC-UL671-00

Open in new window


I tried using this code

<div>
	<a href="http://www.vacpartswarehouse.com/PANASONIC-vacuums-bags-belts-brushes-parts-and-accessories-s/124.htm"> PANASONIC</a> > <a href="http://www.VacPartsWarehouse.com/Panasonic-Upright-Vacuums-s/5385.htm" title="Panasonic Upright Vacuums"> Panasonic Upright Vacuums</a> > <a href="http://www.VacPartsWarehouse.com/MC-V5241-02-s/5411.htm" title="MC-V5241-02"> MC-V5241-02</a><br />
	<a href="http://www.vacpartswarehouse.com/PANASONIC-vacuums-bags-belts-brushes-parts-and-accessories-s/124.htm"> PANASONIC</a> > <a href="http://www.VacPartsWarehouse.com/Panasonic-Upright-Vacuums-s/5385.htm" title="Panasonic Upright Vacuums"> Panasonic Upright Vacuums</a> > <a href="http://www.VacPartsWarehouse.com/MC-UL675-00-s/5424.htm" title="MC-UL675-00"> MC-UL675-00</a><br />
	<a href="http://www.vacpartswarehouse.com/PANASONIC-vacuums-bags-belts-brushes-parts-and-accessories-s/124.htm"> PANASONIC</a> > <a href="http://www.VacPartsWarehouse.com/Panasonic-Upright-Vacuums-s/5385.htm" title="Panasonic Upright Vacuums"> Panasonic Upright Vacuums</a> > <a href="http://www.VacPartsWarehouse.com/MC-UL671-00-s/5425.htm" title="MC-UL671-00"> MC-UL671-00</a><br />
</div>
<script>
	jQuery('div').each(function(){
		var div = $(this);
 		div.html(
			div.text().replace('<a href="http://www\.vacpartswarehouse.com\.com/(((?!/).)*)/\d{3,4}\.htm">(((?!</a>).)*)</a> ', ''
        )
    )
});
</script>

Open in new window


and got the following, all in plain text

PANASONIC > Panasonic Upright Vacuums > MC-V5241-02 PANASONIC > Panasonic Upright Vacuums > MC-UL675-00 PANASONIC > Panasonic Upright Vacuums > MC-UL671-00

Open in new window


I'm pretty inexperienced in both jQuery and regular expressions, so I'm sure I'm missing something simple, but I don't know what. I've attached the test file I'm using in case I'm missing something there.

Thank you in advance for your help,
B
0
Comment
Question by:vacpartswarehouse
[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
3 Comments
 
LVL 13

Expert Comment

by:themrrobert
ID: 39273072
Well when I look at the regular expression, it seems to be doing exactly what you say.

which is find an string like <a href=.....\d{3,4}.>ignore stuff in middle(continuematching:)</a>

so it strips all that out.

maybe you can try matching the 3 instances of link : (ie href="(link)"), and also match the 3 anchor text strings, and then do something like this.

str.replace('newregex','&gt;&gt; <a href="\5">\6</a>')  the \5 matches the 5th match within () parenthesis, and \6 matches the 6th.

Keep that in mind, sorry it's not a more specific solution but javascript regex's isn't my strong suit.
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 39273678
<div>
	<a href="http://www.vacpartswarehouse.com/PANASONIC-vacuums-bags-belts-brushes-parts-and-accessories-s/124.htm"> PANASONIC</a> > <a href="http://www.VacPartsWarehouse.com/Panasonic-Upright-Vacuums-s/5385.htm" title="Panasonic Upright Vacuums"> Panasonic Upright Vacuums</a> > <a href="http://www.VacPartsWarehouse.com/MC-V5241-02-s/5411.htm" title="MC-V5241-02"> MC-V5241-02</a><br />
	<a href="http://www.vacpartswarehouse.com/PANASONIC-vacuums-bags-belts-brushes-parts-and-accessories-s/124.htm"> PANASONIC</a> > <a href="http://www.VacPartsWarehouse.com/Panasonic-Upright-Vacuums-s/5385.htm" title="Panasonic Upright Vacuums"> Panasonic Upright Vacuums</a> > <a href="http://www.VacPartsWarehouse.com/MC-UL675-00-s/5424.htm" title="MC-UL675-00"> MC-UL675-00</a><br />
	<a href="http://www.vacpartswarehouse.com/PANASONIC-vacuums-bags-belts-brushes-parts-and-accessories-s/124.htm"> PANASONIC</a> > <a href="http://www.VacPartsWarehouse.com/Panasonic-Upright-Vacuums-s/5385.htm" title="Panasonic Upright Vacuums"> Panasonic Upright Vacuums</a> > <a href="http://www.VacPartsWarehouse.com/MC-UL671-00-s/5425.htm" title="MC-UL671-00"> MC-UL671-00</a><br />
</div>
<script>
	jQuery('div').each(function(){
		var div = $(this);
		var result=div.html().match( /(<a[^>]+>[^<>]+<\/a>)<br\s*\/?>/gi );
		div.html( "&gt;&gt; " + result.join("&gt;&gt; ") );

	});
</script>

Open in new window

0
 

Author Closing Comment

by:vacpartswarehouse
ID: 39275588
That worked perfectly! Thank you so much for your help!
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Anyone familiar with PhotoSwipe? 3 42
Angular2 remove duplicates 11 24
Different Javascript on iPhone than Win 10 desktop? 7 53
Error just appeared 3 23
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 …
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 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)

734 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