Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
472 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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
ID: 34940474
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one 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 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…
Suggested Courses

916 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