2017-01-20 22 views
6

Mam listę skrótów:regex - efektywnie wykorzystać wszystkie skróty z danej listy w tekście

var shortcuts = ["efa","ame","ict","del","aps","lfb","bis","bbc"... 

i ciało tekstów o różnej kapitalizacji:

var myText = "Lorem ipsum... Efa, efa, EFA ..."; 

Czy to możliwe, aby zastąpić wszystkie słowa w tekście pasujące do listy skrótów za pomocą skrótu wersji skrótu za pomocą wyrażenia regularnego? Czy można to zrobić bez pętli tylko przy użyciu String.prototype.replace()?

pożądany rezultat w moim przykładzie będzie:

myText = "Lorem ipsum... EFA, EFA, EFA ..."; 

Odpowiedz

6

generuje pojedynczy regex z tablicy sznurka i zastąpić ciąg przy użyciu metody String#replace z zwrotnego.

var shortcuts = ["efa", "ame", "ict", "del", "aps", "lfb", "bis", "bbc"]; 
 

 
var myText = "Lorem ipsum... Efa, efa, EFA ..."; 
 

 
// construct the regex from the string 
 
var regex = new RegExp(
 
    shortcuts 
 
    // iterate over the array and escape any symbol 
 
    // which has special meaning in regex, 
 
    // this is an optional part only need to use if string cotains any of such character 
 
    .map(function(v) { 
 
    // use word boundary in order to match exact word and to avoid substring within a word 
 
    return '\\b' + v.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&') + '\\b'; 
 
    }) 
 
    
 
    // or you can use word boundary commonly by grouping them 
 
    // '\\b(?:' + shortcuts.map(...).join('|') + ')\\b' 
 
    
 
    // join them using pipe symbol(or) although add global(g) 
 
    // ignore case(i) modifiers 
 
    .join('|'), 'gi'); 
 

 
console.log(
 
    // replace the string with capitalized text 
 
    myText.replace(regex, function(m) { 
 
    // capitalize the string 
 
    return m.toUpperCase(); 
 
    }) 
 
    // or with ES6 arrow function 
 
    // .replace(regex, m => m.toUpperCase()) 
 
);


Patrz: Converting user input string to regular expression

+1

niesamowite. Czy mógłbyś wytłumaczyć to? –

+0

@ daniel.sedlacek: dodano pewne wyjaśnienie –

+1

Powinieneś dodać granice słów wokół skrótów w regex, w przeciwnym razie to przekształci słowa takie jak "ameryka" w "AMErica" ​​ –

0

proste podejście bez join:

var shortcuts = ["efa","ame","ict","del","aps","lfb","bis","bbc"], myText = "Lorem ipsum... Efa, efa, EFA ..aps apS whatever APS."; 
shortcuts.map((el)=> { 
myText = myText.replace(new RegExp('\\b'+el+'\\b',"gi"), el.toUpperCase()) 
}); 
console.log(myText); 
2

Zakładając kontrolować początkową tablicę skrótów i wiesz, że to tylko cd NLPZ znaków:

const shortcuts = ["efa","ame","ict","del","aps","lfb","bis","bbc"] 
 

 
var text = "Lorem ipsum... Efa, efa, EFA, ame, America, enamel, name ..." 
 

 
var regex = new RegExp("\\b(" + shortcuts.join('|') + ")\\b", 'gi') 
 

 
console.log(text.replace(regex, s => s.toUpperCase()));

W \b granice będą unikać zastępując skróty wewnątrz wyrazów.

+1

musisz użyć 'join() '\\ b | \\ b') ' –

+1

lub' "\\ b (" + shortcuts.join ('|') + ") \\ b" ' –

+0

Rzeczywiście. Dzięki. –