javascript regexp replace stuff wrapped in quotes?

I know some basic regexp, but here's the thing. Every charachter will be enclosed in spans. I want things that are surrounded by quotes to be replaced, so, something like "something" would actually be

<span class="charachter">"</span>
<span class="charachter">s</span>
<span class="charachter">o</span>
<span class="charachter">m</span>
<span class="charachter">e</span>
<span class="charachter">t</span>
<span class="charachter">h</span>
<span class="charachter">i</span>
<span class="charachter">n</span>
<span class="charachter">g</span>
<span class="charachter">"</span>

(The line breaks are there just for convinience. It's just 1 long line in the real thing.)

How do I make that into:

<span class="charachter green">"</span>
<span class="charachter green">s</span>
<span class="charachter green">o</span>
<span class="charachter green">m</span>
<span class="charachter green">e</span>
<span class="charachter green">t</span>
<span class="charachter green">h</span>
<span class="charachter green">i</span>
<span class="charachter green">n</span>
<span class="charachter green">g</span>
<span class="charachter green">"</span>

? I'm using regexp because that "something" might be anything.

BTW: The use of jQuery in the code is allowed

There are more things that are .charachter class that I don't want to add class "green" to, just the ones enclosed in quotes

Answers


This might work, depending on the complexity of your page:

var quotes = $("span.charachter:contains('\"')");
for (var i = 0; i < quotes.length - 1; i++) {
    quotes.eq(i).nextUntil(quotes.eq(++i)).andSelf().addClass("green");
}

Working demo: http://jsfiddle.net/3SRdD/2/

This assumes that:

  • A span.charachter will only ever contain one single character.
  • You will never have an unmatched quote.
  • The following siblings of a span.charachter with a quote are always more span.charachter elements, at least until the closing quote.

Need Your Help

Slider menu View issue

android view slider viewgroup android-sliding

I have implemented a ListView with headers from sqlite in a separate project which is like this:

Can a library built in standard Linux be used in Android?

android android-ndk jni glibc

I wrote a library in latest Linux distribution, which depends on glibc and sockets. I hear that glibc is not supported in Android. Is there a way that I rebuild my code without much modification for

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.