D
D
DanilkaEs62018-07-28 12:14:50
JavaScript
DanilkaEs6, 2018-07-28 12:14:50

How to find common matches of two regular expressions?

Hello.
Imagine we have a line:
const text = 'hemidemisemiquaver';
We also have two masks:
const s1 = 'emi';
const s2 = 'm.se';
Matches of the first mask should be in italics and the second in bold.
You also need to take into account that if the match applies to the first and second at the same time, then this match must have both styles.
In this example, it will be like this h emi d e mi s e mi quaver
In general, I broke my whole head, but I can’t implement it.
Help with an idea or example code, please)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Anton Shamanov, 2018-07-28
@SilenceOfWinter

php has similar_text and soundex , look for analogues for js.
Calculates the degree of similarity of two strings using the algorithm described in Programming Classics: Implementing the World's Best Algorithms by Oliver (ISBN 0-131-00413-1).

X
xmoonlight, 2018-07-28
@xmoonlight

General - can not be.
Because there will be a violation of the correctness of the tag, for example: <b><i>общее</b></i>
You decide: in what order you add tags: first italic, then bold, or vice versa.
When performing the 2nd step, you are already doing the operation on the TOKENS (without tags) obtained in the 1st step, and not on the original string, in order to avoid intersections when tagging (as in the example above).

T
Timokins, 2018-07-28
@timokins

An idea in 5 minutes, I don’t pretend to be beautiful and lack a bicycle:
create a new variable text_html , in which each letter from text will be wrapped in a tag

let text_html = '<span>h</span><span>e</span> ... <span>r</span>';
// создаете её программно, естественно, работая с DOM

then look for the masks in the original text and get the indices and length, access the text_html
using the obtained indices and length , find the necessary elements and style them after with text_html, do what you want, you can put in order by removing the extra span something like this

N
Niriter Inc., 2018-07-28
@niriter

can't get through it ? Or did I mess something up?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question