Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function highlightStr(name, query) {
- if (!query)
- return [
- {
- str: name,
- isHighlight: false
- }
- ];
- const queryRegx = new RegExp(query, "ig");
- const matches = name.match(queryRegx);
- if (!matches)
- return [
- {
- str: name,
- isHighlight: false
- }
- ];
- const textSplits = name.split(queryRegx).reduce((acc, item) => {
- item &&
- acc.result.push({
- str: item,
- isHighlight: false
- });
- matches[acc.matchIndex] &&
- acc.result.push({
- str: matches[acc.matchIndex],
- isHighlight: true
- });
- acc.matchIndex += 1;
- return acc;
- }, { result: [], matchIndex: 0 });
- return textSplits.result;
- }
- test('ABCDEFABKDEAbSB', 'ab')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement