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
461 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
[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
  • 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
Industry Leaders: 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 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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)
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

623 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