1

For example, I have the string:

<template>
    <view class="container">{{title}}</view>
</template>

or

<custom>
    anything...
</cusomt>

I just want to remove template/custom tag and keep the tags inside template/custom.

The method in Remove HTML Tags in Javascript with Regex will remove all tags.

Note: I'm not developing a web page. I just use node to process string.

tomfriwel
  • 2,575
  • 3
  • 20
  • 47
  • 4
    Don't use regex to parse html. – user3483203 Mar 12 '18 at 07:22
  • I just want using regex extract the content inside tag. Just remove the string of outermost tag. – tomfriwel Mar 12 '18 at 07:26
  • 1
    In general regex is not meant to match patterns with matching closing and opening sequences (like tags or quotes). Some extensions to standard regex can allow for this but often result in clunky and complicated regexes. You will be much better off using the built-in DOM parser. – define cindy const Mar 12 '18 at 08:09
  • Don't use a regular expression to parse HTML, see [*RegEx match open tags except XHTML self-contained tags*](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). – RobG Mar 12 '18 at 08:12
  • Okay, I got it. So, there's other solution in `node`? – tomfriwel Mar 12 '18 at 08:28
  • 1
    @tomfriwel—use a DOM parser, modify the DOM as required, then maybe serialise it so you get the HTML back (it might not be identical markup but should be equivalent). See answers. – RobG Mar 12 '18 at 08:39

2 Answers2

0

Since this is Javascript, why not use something like document.getElementsByTagName('template') and then extract the children?

var templateElements = document.getElementsByTagName('template');
var processedElements = [];

for (var i = 0; i < templateElements.length; i++) {
    var templateParent = templateElements[i];
    var children = templateParent.children;
    processedElements.push(children);
}
Thomas Jiang
  • 1,303
  • 11
  • 16
0

You can use the DOM Parser API as regex aren't supposed to parse XML or HTML,

let s = `<template>
    <view class="container">{{title}}</view>
</template>`;
let s1 = `<custom>
    anything...
</custom>`;
let d = new DOMParser();
let doc = d.parseFromString(s, 'application/xml');
let doc1 = d.parseFromString(s1, 'application/xml');
let r = doc.children[0].innerHTML;
console.log(r);
let r1 = doc1.children[0].innerHTML;
console.log(r1)

Edit

As you are in a node environment but I would still recommend you to use a DOM parsing library.

You could also use /\<.*\>([\s\S]*)\<\/.*\>/. This regex doesn't check for validity but assumes the provided markup is valid.

Demo

let s = `<template>
<view class="container">{{title}}</view>
</template>`;
let s1 = `<custom>
    anything...
</custom>`;
let re = /\<.*\>([\s\S]*)\<\/.*\>/
console.log(re.exec(s)[1].trim());
console.log(re.exec(s1)[1].trim());

Edit2

You can also use cheerio like below :-

const cheerio = require('cheerio');
let s = `<template>
<view class="container">{{title}}</view>
</template>`;
let $ = cheerio.load('<div id="myid">' + s + '</div>');
console.log($('#myid>:nth-child(1)').html().trim());
vibhor1997a
  • 2,336
  • 2
  • 17
  • 37