javascript regexp replace stuff wrapped in quotes? - javascript

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

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.

Simple jquery solution - something like?
$(".character").addClass("green")

if your pattern looks like this /"(.*)"/ you should place a question mark ? after the star * to make it ungreedy, like this /"(.*?)"/
also checkout repeating section on regular-expressions.info

You can't do it reliably, because " shows up inside the span as well.
But if you willing to be more restrictive you can search for <span class=".*" - but it will only match tags that look exactly like that.

Related

How to get a "data-value” attribute updated in angular 2

Got a data-attribute bound to an Angular2 variable. That works fine. But a method might change the value, which is MOSTLY not reflected in the data-attribute. Any idea how to solve this? Here a simplified example:
<span (click)="addMoney(item)">
<i class="money inline icon right" attr.data-content="Click amount to donate ${{item.step}}"></i>
</span>
So, in this example, assume that addMoney() can change "item.step".
Thanks in advance for any help.
Just wrap attr.data-content in square brackets and string value in single quotes like below and remove curly brackets.
<span (click)="addMoney(item)">
<i class="money inline icon right"
[data-content]="'Click amount to donate ' + item.step"></i>
</span>

replace text, but ignore a certain string inside the document

I have a big string: Hello <span class="ashakd">my</span> name is <bob>!
I have a second string: llo my name
I have what i want to replace it with:<span class="ashakd">llo my name</span>
I need to replace() it as if the <span class="ashakd"> and </span> didnt exist, but they are replaced with the string so the final result is: He<span class="ashakd">llo my name</span> is <bob>!
PS: <bob> exists so you cant ignore any text between two >'s it must specifically ignore <span class="ashakd"> and </span>
very sorry if this is confusing. ask me to make it clearer if this is confusing
edit
sorry for being unclear, but it must only replace the within my replace. so if the original string was: Hello <span class="ashakd">my</span> name is <bob><span class="ashakd">hello</span>!
the result would be: He<span class="ashakd">llo my name</span> is <bob><span class="ashakd">hello</span>!
This may be too destructive to the original string, but I propose this solution:
var a = 'Hello <span class="ashakd">my</span> name is <bob>!';
var searchString = 'llo my name';
// remove all <span> and </span> tags, you may not want to remove any and all span tags???
a = a.replace(/<\/?span[^>]*?>/g,'');
a = a.replace(searchString,"<span class='ashakd'>"+searchString+"</span>");
What this does is remove all span tags, then search for your "llo my name" search string, and wrap that with a span tag.
Since you said you don't know regex that well, here's a description of:
/<\/?span[^>]*?>/g
<\/? means match on '<' and then optionally a /. This matches both the start and end tags, i.e. <span...> and </span>
[^>]*? means match any character that is NOT > in a non-greedy fashion, i.e. stop matching at the first > found.
The final /g means 'global', which means match <span> and </span> as many times as possible.

Prototype script $$('span[tab_caption="abcde"]')[0].select('span[id*=section.]')[0]

Can anyone please help to explain what does this code do? Specifically what does "id*=section." mean?
Prototype script $$('span[tab_caption="abcd"]')[0].select('span[id*=section.]')[0]
I suspect the '.' might be a typo but i'm not sure. What this appears to be doing is find the first span with attribute tab_caption=abcd then within that element, find the first span with id of *section*.
When I try this with the trailing '.', I get an error from prototype saying Syntax error, unrecognized expression: [id*=section.]". But without the '.' things work out.
Looking at a sample piece of DOM like this
<span tab_caption="ac">
span ac
<span id="section1">section1</span>
</span>
<span tab_caption="ad">
span ad
<span id="section2">section2</span>
</span>
<span tab_caption="ac">
span ac
<span id="section3">section3</span>
</span>
<span tab_caption="ad">
span ad
<span id="section4">section4</span>
</span>
Then running those selectors in the console I get:
> $$('span[tab_caption="ac"]')[0].select('span[id*=section]')[0]
<span id=​"section1">​section1​</span>​
> $$('span[tab_caption="ad"]')[0].select('span[id*=section]')[0]
<span id=​"section2">​section2​</span>​
As you can see, the first query picked the first span with tab_caption=ac and gave me section1. The second picked the first from tab_caption=ad and gave me the first section* match which was section2.
Are you sure the '.' is supposed to be in the select argument?
This attribute selector was added with CSS3 and matches any attribute which contains the specified substring. In your case, any span element which has an id attribute with the string section. somewhere in it.

jQuery highlight pieces of text in an element across tags

I want to select and return searched text using jQuery.
The problem is; parts of the text may be located in <span> or other inline elements, so when searching for 'waffles are tasty' in this text: 'I'm not sure about <i>cabbages</i>, but <b>waffles</b> <span>are</span> <i>tasty</i>, indeed.', you wouldn't get any matches, while the text appears uninterrupted to people.
Let's use this HTML as an example:
<div id="parent">
<span style="font-size: 1.2em">
I
</span>
like turtles
<span>
quite a
</span>
lot, actually.
<span>
there's loads of
</span>
tortoises over there, OMG
<div id="child">
<span style="font-size: 1.2em">
I
</span>
like turtles
<span>
quite a
</span>
lot, actually.
TURTLES!
</div>
</div>
With this (or similar) JavaScript:
$('div#parent').selectText({query: ['i like', 'turtles', 'loads of tortoises'], caseinsensitive: true}).each(function () {
$(this).css('background-color', '#ffff00');
});
//The (hypothetical) SelectText function would return an array of wrapped elements to chain .each(); on them
You would want to produce this output: (without the comments, obviously)
<div id="parent">
<span style="font-size: 1.2em">
<span class="selected" style="background-color: #ffff00">
I
</span> <!--Wrap in 2 separate selection spans so the original hierarchy is disturbed less (as opposed to wrapping 'I' and 'like' in a single selection span)-->
</span>
<span class="selected" style="background-color: #ffff00">
like
</span>
<span class="selected" style="background-color: #ffff00"> <!--Simple match, because the search query is just the word 'turtles'-->
turtles
</span>
<span>
quite a
</span>
lot, actually.
<span>
there's
<span class="selected" style="background-color: #ffff00">
loads of
</span> <!--Selection span needs to be closed here because of HTML tag order-->
</span>
<span class="selected" style="background-color: #ffff00"> <!--Capture the rest of the found text with a second selection span-->
tortoises
</span>
over there, OMG
<div id="child"> <!--This element's children are not searched because it's not a span-->
<span style="font-size: 1.2em">
I
</span>
like turtles
<span>
quite a
</span>
lot, actually.
TURTLES!
</div>
</div>
The (hypothetical) SelectText function would wrap all selected text in <span class="selected"> tags, regardless of whether parts of the search are located in other inline elements like <span>, '', etc. It does not search the child <div>'s contents because that's not an inline element.
Is there a jQuery plugin that does something like this? (wrap search query in span tags and return them, oblivious to whether parts of the found text may be located in other inline elements?)
If not, how would one go about creating such a function? This function's kinda what I'm looking for, but it doesn't return the array of selected spans and breaks when parts of the found text are nested in other inline elements.
Any help would be greatly appreciated!
Piece of cake! See this.
Folded notation:
$.each(
$(...).findText(...),
function (){
...
}
);
In-line notation:
$(...).findText(...).each(function (){
...
}
);
Three options:
Use the browser's built-in methods for this. For the finding, IE has TextRange with its findText() method; other browsers (with the exception of Opera, last time I checked, which was a long time ago) have window.find(). However, window.find() may be killed off without being replaced at some point, which is not ideal. For the highlighting, you can use document.execCommand().
Use my Rangy library. There's a demo here: http://rangy.googlecode.com/svn/trunk/demos/textrange.html
Build your own code to search text content in the DOM and style it.
The first two options are covered in more detail on this answer:
https://stackoverflow.com/a/5887719/96100
Since I just so happened to be working on a similar thing right now, in case you'd like to see the beginnings of my interpretation of "option 3", I thought I'd share this, with the main feature being that all text nodes are traversed, without altering existing tags. Not tested across any unusual browsers yet, so no warranty whatsoever with this one!
function DOMComb2 (oParent) {
if (oParent.hasChildNodes()) {
for (var oNode = oParent.firstChild; oNode; oNode = oNode.nextSibling) {
if (oNode.nodeType==3 && oNode.nodeValue) { // Add regexp to search the text here
var highlight = document.createElement('span');
highlight.appendChild(oNode.cloneNode(true));
highlight.className = 'selected';
oParent.replaceChild(highlight, oNode);
// Or, the shorter, uglier jQuery hybrid one-liner
// oParent.replaceChild($('<span class="selected">' + oNode.nodeValue + '</span>')[0], oNode);
}
if (oNode.tagName != 'DIV') { // Add any other element you want to avoid
DOMComb2(oNode);
}
}
}
}
Then search through things selectively with jQuery perhaps:
$('aside').each(function(){
DOMComb2($(this)[0]);
});
Of course, if you have asides within your asides, strange things might happen.
(DOMComb function adapted from the Mozilla dev reference site
https://developer.mozilla.org/en-US/docs/Web/API/Node)
I wrote a draft as a fiddle. The main steps:
I made a plugin for jQuery
$.fn.selectText = function(params){
var phrases = params.query,
ignorance = params.ignorecase;
wrapper = $(this);
. . .
return(wrapper);
};
Now I can call the selection as a $(...).selectText({query:["tortoise"], ignorance: true, style: 'selection'});
I know you want to have iterator after the function call, but in your case it is impossible, because iterator have to return valid jQuery selectors. For example:
word <tag>word word</tag> word is not valid selector.
After sanitizing the content of wrapper, for each search makeRegexp() makes personal regular expression.
Each searched piece of html source goes to emulateSelection() and then wrapWords()
Main idea is to wrap in <span class="selection">...</span> each single piece of phrase not separated by tags, but not analyze the whole tree of nodes.
NOTE:
It's NOT working with <b><i>... tags in html. You have to make corrections in regexp string for it.
I not guarantee it will work with unicode. But who knows...
As I understood, we talking about iterators like $.each($(...).searchText("..."),function (str){...});.
Check the David Herbert Lawrence poem:
<div class="poem"><p class="part">I never saw a wild thing<br />
sorry for itself.<br />
A small bird will drop frozen dead from a bough<br />
without ever having felt sorry for itself.<br /></p></div>
Actually, after rendering, browser will understood it like this:
<div class="poem">
<p class="part">
<br>I never saw a wild thing</br>
<br>sorry for itself.</br>
<br>A small bird will drop frozen dead from a bough</br>
<br>without ever having felt sorry for itself.</br>
</p>
</div>
For example, I looking for the phrase: "wild thing sorry for". Therefore, I have to highligt the exerpt:
wild thing</br><br>sorry for
I can not wrap it like this <span>wild thing</br><br>sorry for</span>, then create jQuery selector by some temporary id="search-xxxxxx", and return it back -- it's wrong html. I can wrap each piece of text like this:
<span search="search-xxxxx">wild thing</span></br><br><span search="search-xxxxx">sorry for</span>
Then I have to call some function and return jQuery array of selectors:
return($("[search=search-xxxxx]"));
Now we have two "results": a) "wild thing"; b) "sorry for". Is it really what you want?
OR
You have to write you own each() function like another plugin to jQuery:
$.fn.eachSearch = function(arr, func){
...
};
where arr will be not an array of selectors, but array of arrays of selectors, like:
arr = [
{selector as whole search},
{[{selector as first part of search]}, {[selector as second part of search]}},
...
]

Substring html tag using Javascript

I want to sub string and remove the , which appears within the span tag and display the name alone. Below are the two cases which needs to work.
Case1: <span class="datatableheader">No results found, </span>
Case2: <span class="datatableheader">Jude Gomes, </span>
A single function should help in removing the , in both cases and display the result as
<span class="datatableheader">No results found </span>
<span class="datatableheader">Jude Gomes </span>
Appreciate for any help.
Thanks
$(".datatableheader").html ($(".datatableheader").html().replace(",",""));
It's not widely recognized that .html accepts a callback function:
$('.datatableheader').html(function(i,old) {
return old.replace(/, ?/g, '');
});​
http://jsfiddle.net/3fBY4/1/
you can try this also
var parts = id.split(':'); //because u have case1: or case2:
// it will split into string in array//
$('#parts[1]').replace(",",""));
//try to print that it will work
nice question.

Categories