Can someone help me with a javascript function that can highlight text on a web page. And the requirement is to - highlight only once, not like highlight all occurrences of the text as we do in case of search.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Why using a selfmade highlighting function is a bad idea
The reason why it's probably a bad idea to start building your own highlighting function from scratch is because you will certainly run into issues that others have already solved. Challenges:
innerHTML
)Sounds complicated? If you want some features like ignoring some elements from highlighting, diacritics mapping, synonyms mapping, search inside iframes, separated word search, etc. this becomes more and more complicated.
Use an existing plugin
When using an existing, well implemented plugin, you don't have to worry about above named things. The article 10 jQuery text highlighter plugins on Sitepoint compares popular highlighter plugins.
Have a look at mark.js
mark.js is such a plugin that is written in pure JavaScript, but is also available as jQuery plugin. It was developed to offer more opportunities than the other plugins with options to:
DEMO
Alternatively you can see this fiddle.
Usage example:
It's free and developed open-source on GitHub (project reference).
The solutions offered here are quite bad.
&
for &,<
for <,>
for >,ä
for ä,ö
for öü
for üß
for ß, etc.What you need to do:
Loop through the HTML document, find all text nodes, get the
textContent
, get the position of the highlight-text withindexOf
(with an optionaltoLowerCase
if it should be case-insensitive), append everything beforeindexof
astextNode
, append the matched Text with a highlight span, and repeat for the rest of the textnode (the highlight string might occur multiple times in thetextContent
string).Here is the code for this:
Then you can use it like this:
Here's an example HTML document
By the way, if you search in a database with
LIKE
,e.g.
WHERE textField LIKE CONCAT('%', @query, '%')
[which you shouldn't do, you should use fulltext-search or Lucene], then you can escape every character with \ and add an SQL-escape-statement, that whay you'll find special characters that are LIKE-expressions.e.g.
and the value of @query is not
'% completed'
but'\%\ \c\o\m\p\l\e\t\e\d'
(tested, works with SQL-Server and PostgreSQL, and every other RDBMS system that supports ESCAPE)
A revised typescript-version:
Usage:
I have the same problem, a bunch of text comes in through a xmlhttp request. This text is html formatted. I need to highlight every occurrence.
The problem is that I don't need to highlight text in tags. For example I need to highlight fox:
Now I can replace it with:
To answer your question: you can leave out the g in regexp options and only first occurrence will be replaced but this is still the one in the img src property and destroys the image tag:
This is the way I solved it but was wondering if there is a better way, something I've missed in regular expressions:
Here's my regexp pure JavaScript solution:
I found highlight plugin to be the best match, with it you can highlight part of the content: