Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const make_pattern_builder = function() {
- let chars = [];
- let tags = [];
- return {
- add_chars: function(c) {chars.push(c);},
- add_tag: function(t) {tags.push(`<(?: *)${t}(?: *)>[^<]*<(:? *)/(?: *)${t}(?: *)>`);},
- add_autoclosing_tag : function (t) { tags.push(`<(?: *)${t}(?: *?/? *?)>`) ; },
- get_raw: function() {
- const c = [ `[${chars.join('')}]` ];
- const all = c.concat(tags);
- return all.join('|');
- },
- build: function() {
- return new RegExp(this.get_raw(), "g");
- },
- build_match_all: function() {
- return new RegExp(this.get_raw()+"|.", "g");
- },
- clear_not_matching: function (v){
- const pattern = this.build();
- let parts = null;
- let result = '';
- while(parts = pattern.exec(v)){
- result += parts[0];
- }
- return result;
- }
- }
- };
- // Aqu'i empieza.
- let pb = make_pattern_builder();
- pb.add_chars("a-zA-Z ");
- pb.add_autoclosing_tag("br");
- console.log(pb.clear_not_matching("hola <br> / 6666 que tal"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement