Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
902 views
in Technique[技术] by (71.8m points)

regex - Replace certain arabic words in text string using Javascript

I have a text string in arabic and i want to make some words Bold and add icons before the word

the method I am using for this for english text is

var wordsToBold = ["Properties", "How To Use"];

    function makeBold(input, wordsToBold) {
        return input.replace(new RegExp('(\b)(' + wordsToBold.join('|') + ')(\b)', 'ig'),
            '<br><br><i class="fas fa-ellipsis-h fa-xs mr-2"></i>$1<b>$2</b>$3');
    }

When i use this method with arabic words it does not working

var wordsToBold = ["???????", "????? ?????????"];

A text for testing:

??????? ???? ???????: ????? ?????? ????? ??? ???? ,??????????? ?????? ?????? ??????,??? ?? ??????? ??????

??????????????? ?????? ?? ????? ????? ??????? . ????? ?????????: ?? ???? ?? ????? ??????,????? ???????

????? ?????? ??????? ???????

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can build your own Unicode word boundaries:

  1. Left-hand (leading) word boundary pattern can be defined as a position that is not immediately preceded with letter + any potential diacritics or a digit or an underscore: (?<!p{L}p{M}*|[p{N}_])
  2. Right-hand (trailing) word boundary pattern can be defined as a position that is not immediately followed with a letter, digit or underscore: (?![p{L}p{N}_]).

Thus, you can use

var wordsToBold = ["Properties", "How To Use"];

function makeBold(input, wordsToBold) {
    return input.replace(new RegExp('(?<!\p{L}\p{M}*|[\p{N}_])(?:' + wordsToBold.join('|') + ')(?![\p{L}\p{N}_])', 'igu'),
        '<br><br><i class="fas fa-ellipsis-h fa-xs mr-2"></i><b>$&</b>');
}
console.log(makeBold("How To Use These Properties: 00How To Use These Properties00", wordsToBold));
// => <br><br><i class="fas fa-ellipsis-h fa-xs mr-2"></i><b>How To Use</b> These <br><br><i class="fas fa-ellipsis-h fa-xs mr-2"></i><b>Properties</b>: 00How To Use These Properties00
var wordsToBold = ["???????", "????? ?????????"];
var arString = `??????? ???? ???????: ????? ?????? ????? ??? ???? ,??????????? ?????? ?????? ??????,??? ?? ??????? ??????

??????????????? ?????? ?? ????? ????? ??????? . ????? ?????????: ?? ???? ?? ????? ??????,????? ???????

????? ?????? ??????? ???????`;
console.log(makeBold(arString, wordsToBold));
// => ??????? ???? <br><br><i class="fas fa-ellipsis-h fa-xs mr-2"></i><b>???????</b>: ????? ?????? ????? ??? ???? ,??????????? ?????? ?????? ??????,??? ?? ??????? ?????? ??????????????? ?????? ?? ????? ????? ??????? . <br><br><i class="fas fa-ellipsis-h fa-xs mr-2"></i><b>????? ?????????</b>: ?? ???? ?? ????? ??????,????? ??????? ????? ?????? ??????? ???????

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...