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
456 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building JSON/JQuery Results Display 11 54
How to open a new browser tab after executing php script 20 49
Error Handler 8 42
Why is my select returning NaN 23 39
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

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