Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Latest commit

 

History

History
31 lines (22 loc) · 854 Bytes

string_highlight.md

File metadata and controls

31 lines (22 loc) · 854 Bytes

strings/stringHighlight

Wraps the given word in HTML tags where found in the given string.

function stringHighlight(str: string, word: string, tag: string = 'i'): string

Args

str:string
String to search through for the given word.

word:string
The word to find and highlight in the given string.

tag:string
Wrap the word in this HTML tag, e.g. 'i', 'strong', 'span', etc

Returns

A string with the word wrapped in the specified tag.

Examples

import { stringHighlight } from 'utils/strings';

console.log(stringHighlight('foo bar', 'foo'));           // Outputs: '<i>foo</i> bar'
console.log(stringHighlight('Foo bar foo', 'foo'));       // Outputs: '<i>Foo</i> bar <i>foo</i>'
console.log(stringHighlight('foo bar', 'foo', 'strong')); // Outputs: '<strong>foo</strong> bar'