473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Highlight text with Javascript

Hi!

Is it possible, using Javascript, to highlight (in yellow..) all the
occurrences of "number: [0-9]+" (regex) of the document?

Merx
Jul 23 '05 #1
5 3849
Merx wrote:
Hi!

Is it possible, using Javascript, to highlight (in yellow..) all the
occurrences of "number: [0-9]+" (regex) of the document?


Yes.
Jul 23 '05 #2
> Merx wrote:
Hi!

Is it possible, using Javascript, to highlight (in yellow..) all the
occurrences of "number: [0-9]+" (regex) of the document?


Yes.


Good.

In a search on the web I found this page
http://www.nsftools.com/misc/SearchAndHighlight.htm that describe how to do
it using normal (not regex) search terms.

They wrote:

// we're not using a
// regular expression search, because we want to filter out
// matches that occur within HTML tags and script blocks, so
// we have to do a little extra validation

What does this mean? that is not directly possible to make a regex search on
rendered text ignoring HTML tags and script blocks?

Merx
Jul 23 '05 #3
Ivo
"Merx" wrote
Is it possible, using Javascript, to highlight (in yellow..) all the
occurrences of "number: [0-9]+" (regex) of the document?


Sure, enter "number\s\d" (without the quotes) in the input field, check the
"regex" checkbox, and look at the last line of the example text at
<URL: http://4umi.com/web/javascript/hilite.htm >

Below is the function that is called by the form:

function highlight(t, el) {
if (!t) return;
if(!document.getElementById('regex').checked)
t = t.replace(/([\\|^$()[\]{}.*+?])/g, '\\$1');
if (/^\s*$/.test(t)) return;
var r, p = document.getElementById('case').checked? 'g':'gi';
if (document.getElementById('literal').checked) {
r = new RegExp(t, p);
} else {
r = new RegExp(t.split(/\s+/).join('|'), p);
}
var s = [el||document.documentElement||document.body];
var h = document.createElement('span'), i = 0, e, j, l, o, k;
h.style.backgroundColor = '#ff0';
do {
e = s[i];
if (e.nodeType == 3) {
r.lastIndex = 0;
l = r.exec(e.nodeValue);
if (l != null) {
k = l[0].length;
if (r.lastIndex > k) {
e.splitText(r.lastIndex-k);
e = e.nextSibling;
}
if (e.nodeValue.length > k) {
e.splitText(k);
s[i++] = e.nextSibling;
}
o = h.cloneNode(true);
o.appendChild(document.createTextNode(l[0]));
e.parentNode.replaceChild(o, e);
}
} else {
j = e.childNodes.length;
while (j) s[i++] = e.childNodes.item(--j);
}
} while(i--);
}

HTH
Ivo

Jul 23 '05 #4
"Ivo" <no@thank.you> wrote in message
news:41********************@news.wanadoo.nl...
"Merx" wrote
Is it possible, using Javascript, to highlight (in yellow..) all the
occurrences of "number: [0-9]+" (regex) of the document?
Sure, enter "number\s\d" (without the quotes) in the input field, check

the "regex" checkbox, and look at the last line of the example text at
<URL: http://4umi.com/web/javascript/hilite.htm >

Below is the function that is called by the form:

function highlight(t, el) {
[...]


Thanks. It works. But it stop to work well when there are html tags between
the words.
How to ignorate html tags and operate on the rendered text only?

Merx
Jul 23 '05 #5
Merx wrote:

In a search on the web I found this page
http://www.nsftools.com/misc/SearchAndHighlight.htm that describe how to do
it using normal (not regex) search terms.

They wrote:

// we're not using a
// regular expression search, because we want to filter out
// matches that occur within HTML tags and script blocks, so
// we have to do a little extra validation

What does this mean? that is not directly possible to make a regex search on
rendered text ignoring HTML tags and script blocks?
You can isolate textNodes, see W3C dom particulars.
IE uses "element.innerText", but this won't work on many browsers.
Mick
Merx

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: David Morgan | last post by:
Hello I have a little function to highlight text if it exists. Function Highlight(vFind, vSearch) Dim RegEx Set RegEx = New RegExp RegEx.Pattern = vFind RegEx.IgnoreCase = True Highlight =...
5
by: nboutelier | last post by:
Scenario: you enter "foo bar" into a text field... Is it possible through javascript to select/highlight just "foo"? formObject.select() selects all. I need to select only part of the string....
1
by: john woo | last post by:
Hi I'm not good at JS, but want to get more about it. I want to use a JSP (the java code just used to get date, the rest are html and javascript), to display a table. the requirement is the...
2
by: parm | last post by:
How do I highlight text in my first (tabindex = 1) asp control (eg: textbox) on the webform on page load. If I try to set focus using javascript then I get script error "Cannot move focus to...
5
by: Atara | last post by:
I am trying to convert the following code to VB .Net, I still have some gaps (the lines that are marked with (*)) and also I need an ending condition for the while loop. any help would be...
1
by: tranky | last post by:
Hola boys, i've a question for you! It's possible to highlight some text inside a textarea? (not all text, but a portion!) For example, i've this text inside a textarea. INSERISCI TESTO i...
2
by: yes_its_just_me | last post by:
Hi, I've seen this effect on other sites, but I don't know how to do it. What I'm trying to do is enable the user to highlight a portion of text on a webpage, then click an "edit" button and a...
2
by: Celeste | last post by:
Hello, I'm trying to parse the referring url for google search terms so that when this page loads it will scroll to and highlight the search term(s). Should i be using document.referrer? ...
3
by: mukeshrasm | last post by:
hello everyone How can i use this code in php. please help me with php syntex of the correponding code. <tr id=\"TR" + @Text(@DocumentUniqueID) + "\">" "<td><input name=\"Document\"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.