Highlight Multiple Keywords in Search

highlight multiple keywords in search

regular expressions is the way to go!

function highlight($text, $words) {
preg_match_all('~\w+~', $words, $m);
if(!$m)
return $text;
$re = '~\\b(' . implode('|', $m[0]) . ')\\b~';
return preg_replace($re, '<b>$0</b>', $text);
}

$text = '
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
';

$words = 'ipsum labore';

print highlight($text, $words);

To match in a case-insensitive manner, add 'i' to the regular expression

    $re = '~\\b(' . implode('|', $m[0]) . ')\\b~i';

NB: for non-enlish letters like "ä" the results may vary depending on the locale.

How to highlight multiple keywords/words in a string with Regex?

If I'm not mistaken, your basically wanting to find each word that is a word in your keywords variable and match them in your string so you can wrap them in a span.

You'll want to first turn your keywords into a RegExp, then do a global match. Something like this:

const keywordsString = "cake pie cookies";const keywords = keywordsString.split(/\s/);
// equivalent to: /(cake|pie|cookies)/gconst pattern = new RegExp(`(${keywords.join('|')})`, 'g');
const phrase = "I like cake, pie and cookies";
const result = phrase.replace(pattern, match => `<span>${match}</span>`);
console.log(result);

How to search multiple keywords in paragraph and highlight the each keyword with different color in javascript using ReactJS?

You can first define an object to hold the color for each of the keyword, then use it later

const styles = {
"sea": {
fontWeight: "bold", color: "red"
},
"sky": {
fontWeight: "bold", color: "blue"
}
}

// use it
<span style={styles[keyword] || {}}>...</span>

Next nested looping of of both parts and KeywordsTosearch is causing the randomly repeated text

parts.map((part, i) => KeywordsTosearch.map((keyword,i) => { })

Since 'KeywordsTosearch' had already done it's job in splitting the sentence, you just need to loop 'parts' to apply the styles

var tempKeyword = [];
tempKeyword = parts.map((part, i) =>
<span
key={i}
style={this.getStyle(part)}>
{part}
</span>
)

See working example https://codesandbox.io/s/recursing-turing-7m56b

highlight multiple keywords in search (PHP)

For multiple search with like should use regexp

For example

SELECT * from products where name REGEXP 'apple|table'; 

Is there an easy to use software to highlight multiple keywords in a text and show count of each match?

I'd say with Vim Multiple Search script this is nearly done, except for counting. But this SO question tackles counting already.

If Vim is not your cup of tea, then I'd say Notepad++ (excerpt added from my comment above):

It certainly can highlight different tokens in different colors (you can configure colors as you see fit), I don't know about counting. The difference might be in usage - IIRC Notepad++ hightlights on keypress / click on token - so you may need to find it first.

Highlight multiple keywords in a string ignoring the added HTML in C#

Try this simple change:

public static string HighlightKeywords(this string input, string keywords)
{
if (input == String.Empty || keywords == String.Empty)
{
return input;
}

return Regex.Replace(
input,
String.Join("|", keywords.Split(' ').Select(x => Regex.Escape(x))),
string.Format("<span class=\"highlight\">{0}</span>", "$0"),
RegexOptions.IgnoreCase);
}

Let Regex do the work for you.

With your input "The class is high".HighlightKeywords("class high") you get "The <span class="highlight">class</span> is <span class="highlight">high</span>" out.

How do I search and highlight multiple terms in Microsoft Word?

Use your current routine as a function.

Here is an example.

Function FindAndMark(sText As String) ' UsingTheFindObject_Medium()
' https://stackoverflow.com/questions/69633517/how-do-i-search-and-highlight-multiple-terms-in-microsoft-word
' Charles Kenyon
'Declare Variables.
Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document

'Grab the ActiveDocument.
Set wrdDoc = Application.ActiveDocument

'Define the Content in the document
Set wrdRng = wrdDoc.Content

'Define the Find Object based on the Range.
Set wrdFind = wrdRng.Find

'Define the parameters of the Search.
With wrdFind
'Search the text for the following term(s)
.Text = sText
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
' Mark text
Do While wrdFind.Execute = True
'Change the color to Yellow.
wrdRng.HighlightColorIndex = wdYellow
Loop
Set wrdFind = Nothing
Set wrdRng = Nothing
Set wrdDoc = Nothing
End Function

Sub MultiFindMark()
' https://stackoverflow.com/questions/69633517/how-do-i-search-and-highlight-multiple-terms-in-microsoft-word
' Charles Kenyon
Dim i As Integer
Const n As Integer = 4 ' set number (n) of terms in search
Dim sArray(n) As String ' Create array to hold terms
' Assign values, starting at 0 and going to n-1
Let sArray(0) = "Aenean"
Let sArray(1) = "Pellentesque"
Let sArray(2) = "libero"
Let sArray(3) = "pharetra"
For i = 0 To n - 1
FindAndMark (sArray(i))
Next i

End Sub

Here is a revision using the code from ASH to handle the Array

Sub MultiFindMark2() 
' https://stackoverflow.com/questions/69633517/how-do-i-search-and-highlight-multiple-terms-in-microsoft-word
' Charles Kenyon
' modified to use methods proposed by ASH
Dim i As Long
Dim sArray() As String ' Create array to hold terms
' Assign values, starting at 0 and going to n-1
sArray = Split("Aenean Pellentesque libero pharetra") ' your list separated by spaces
For i = 0 To UBound(sArray)
FindAndMark (sArray(i))
Next i

End Sub

With some of the changes showing as comments:

Sub MultiFindMark2() 
' https://stackoverflow.com/questions/69633517/how-do-i-search-and-highlight-multiple-terms-in-microsoft-word
' Charles Kenyon
' modified to use methods proposed by ASH
Dim i As Long
' Const n As Integer = 4 ' set number (n) of terms in search
Dim sArray() As String ' Create array to hold terms
' Assign values, starting at 0 and going to n-1
sArray = Split("Aenean Pellentesque libero pharetra") ' your list separated by spaces
' Let sArray(0) = "Aenean"
' Let sArray(1) = "Pellentesque"
' Let sArray(2) = "libero"
' Let sArray(3) = "pharetra"
For i = 0 To UBound(sArray)
FindAndMark (sArray(i))
Next i

End Sub

Note, this still requires the function.

how to use react-highlight-words to highlight multiple keywords with different colors

const Highlight = ({ children, highlightIndex }) => (
<span className={`highlighted-text keyword-${highlightword.indexOf(children)}`}>{children}</span>
);

and add tag - highlightTag={Highlight}

Highlight multiple keywords advanced

<?php
$searchtext = "I have max system performance";
$searchstrings = "max f";
$searchstrings = preg_replace('/\s+/', ' ', trim($searchstrings));
$words = explode(' ', $searchstrings);
$highlighted = array();
foreach ( $words as $word ){
$highlighted[] = "<font color='#00f'><b>".$word."</b></font>";
}
echo strtr($searchtext, array_combine($words, $highlighted));
?>


Related Topics



Leave a reply



Submit