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
446 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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:ddsh79
Comment Utility
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
Comment Utility
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 74

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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)

762 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

10 Experts available now in Live!

Get 1:1 Help Now