Solved

JavaScript regular expressions: How do I replace the outer tag, but not the inner tag similar to it?

Posted on 2011-02-20
10
452 Views
Last Modified: 2012-05-11
Hello,
My question wording may be a bit off, but here's what I'm trying to do:

I'm trying to convert this:
This is a <span class="rcPitchPlus25" title="pitch: +25%"><span class="rcRatePlus25" title="rate: +0.25">test</span></span>.

Open in new window


Into this:
This is a <prodosy pitch="+25%"><prodosy rate="+0.25">test</prodosy></prodosy>.

Open in new window


with this:
<script type="text/javascript">
var content = ''<span class="rcPitchPlus25" title="pitch: +25%"><span class="rcRatePlus25" title="rate: +0.25">test</span></span>.;

function rep(re, str) {
	content = content.replace(re, str);
}

rep(/<span.*?title=\"rate: (.*?)\">(.*?)<\/span>/gi, '<prodosy rate="$1">$2</prodosy>');
rep(/<span.*?title=\"pitch: (.*?)\">(.*?)<\/span>/gi, '<prodosy pitch="$1">$2</prodosy>');
alert(content);
</script>

Open in new window


which is sadly resulting as this:
This is a <prodosy rate="+0.25">test</prodosy></span>.

Open in new window


It's close! I'm just not familiar enough with regex yet to not let it over-match too much in a string. Any thoughts on how to get back on the right track?

Thanks for your expertise in advance! :D

Chris
0
Comment
Question by:Ultrus
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34940423
you can do it without regular expression, using jquery

1) take out the inner HTML separately
http://api.jquery.com/html/
2) replace outer html with another one
http://api.jquery.com/replaceWith
3) restore the inner html
http://api.jquery.com/html/

0
 
LVL 10

Author Comment

by:Ultrus
ID: 34940457
That's true, but I can see some other challenges once I start throwing in all my other curve balls plus it complicates my situation overall. I'm using this for a TinyMCE custom plugin and don't want to require another library to make my little plugin work. I'm hoping this learn something  about regex from this if it's not too much trouble. :)

breaking down my first regular expression into a smaller one:
/<span.*?title=\"rate: /gi

In the string I see it matching all this:
'<span class="rcPitchPlus25" title="pitch: +25%"><span class="rcRatePlus25" title="rate: '

The first '.*?' seems to be troublesom. Any thoughts on how to narrow down my matching expression?

Thanks again,

Chris
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34940474
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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 75

Expert Comment

by:käµfm³d 👽
ID: 34940476
You are trying to parse nested structures via regular expressions, which is a difficult thing to do. When the source data is simple, you can typically achieve this, but the difficulty grows with the amount of nesting.

If you can guarantee that a "pitch" will always come before a "rate", then perhaps you could craft a regex to achieve what you seek. I would strongly suggest going with a method such as that offered by gurvinder372.
0
 
LVL 10

Author Comment

by:Ultrus
ID: 34940491
For more perspective, let me send a link to a soon-to-be open source text-to-speech text editor (problem also included, try clicking the link at the bottom after toying with it a bit):
http://www.christopherstevens.cc/voice/test5.htm

I can't really control the order of things. Perhaps I need to break my regex down a bit more?

Is there a way to say: 'match all characters with but >'?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34940514
Here is what I was trying out... but again, it will have difficulty if you can't guarantee that one will always come before the other (and there are no intermediate </span> tags in between):
<script type="text/javascript">
	var content = '<span class="rcPitchPlus25" title="pitch: +25%"><span class="rcRatePlus25" title="rate: +0.25">test</span></span>';

	function rep(re, str) {
		content = content.replace(re, str);
	}

	rep(/<span [^>]*title="rate: ([^">]+)">(.*?)<\/span>/gi, '<prodosy rate="$1">$2</prodosy>');
	rep(/<span [^>]*title="pitch: ([^">]+)">(.*)<\/span>/gi, '<prodosy pitch="$1">$2</prodosy>');
	alert(content);
</script>

Open in new window

0
 
LVL 10

Author Comment

by:Ultrus
ID: 34940529
kaufmed,
Thanks for your time on that. :)

I'll digest and absorb what you posted. So far: very helpful towards a cure to the crick in my neck.

All,
Thanks for your feedback. I'll get back after some sleep and further reading/testing.

Best regards,

Chris
0
 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 34940540
reverse the order and it should work fine

rep(/<span.*?title=\"pitch: (.*?)\">(.*?)<\/span>/gi, '<prodosy pitch="$1">$2</prodosy>');
           rep(/<span.*?title=\"rate: (.*?)\">(.*?)<\/span>/gi, '<prodosy rate="$1">$2</prodosy>');
0
 
LVL 10

Author Closing Comment

by:Ultrus
ID: 34944405
Yay! It works great with any combination I throw at it in the editor (TinyMCE has some nice features to ensure the structure stays intact). It should work in the link above with refresh... until I break something else. I also learned a bit more about regex in JavaScript. Thanks very much for your awesome feedback. :)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34945460
NP. Glad to help  = )
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i make 2 columns? 4 41
Google Analytics setup 5 50
JQuery Syntax... 4 38
Review of the nodejs code 1 17
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-…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
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…

828 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