简体   繁体   English

移除<p>使用 javascript 从数组中标记</p>

[英]Removing <p> tag from an array using javascript

I have an array as ["<p>123</p>", "<p>124</p>", "<p>125</p>"] and i want to remove the <p> tags as below.我有一个数组为["<p>123</p>", "<p>124</p>", "<p>125</p>"]并且我想将<p>标签删除为以下。

[123, 124, 125] 

I'm expecting it to be a dynamic function to handle the same using javascript.我希望它是一个动态的 function 以使用 javascript 处理相同的问题。 Please help me to resolve the same.请帮我解决同样的问题。

 let output = ["<p>123</p>", "<p>124</p>", "<p>125</p>"].map( item => parseInt(item.replace("<p>", "").replace("</p>", ""))); console.log(output);

I would either extract the numbers or replace the tags.我要么提取数字,要么替换标签。

I added examples for both.我为两者添加了示例。 And I added parseInt , because I assume you want to have integers in the end.我添加了parseInt ,因为我假设你希望最后有整数。 If you want to have strings, just remove it.如果你想有字符串,只需删除它。

Here is an example with replacing the tags:以下是替换标签的示例:

 let data = ["<p>123</p>", "<p>124</p>", "<p>125</p>"] let result = data.map(x => parseInt(x.replace(/<\/?p>/g, ""))); // Note: for any tags, you could use the regex /<\/?.+?>/g instead console.log(result);

And another example extracting the numbers:另一个提取数字的例子:

 let data = ["<p>123</p>", "<p>124</p>", "<p>125</p>"] let result = data.map(x => parseInt(x.replace(/\D*(\d+)\D*/g, "$1"))); console.log(result);

Slicers切片机

 const data = ["<p>123</p>", "<p>124</p>", "<p>125</p>"] const result = data.map(x => parseInt(x.slice(3,x.length-3))); console.log(result);

You could use .replace() with a regular expression to capture the <p> and </p> strings and remove them with the following regex:您可以使用带有正则表达式的.replace()来捕获<p></p>字符串,并使用以下正则表达式删除它们:

/<\/?p>/g

Regex test正则表达式测试

 let data = [ "<p>123</p>", "<p>124</p>", "<p>125</p>" ]; let result = data.map(el => el.replace(/<\/?p>/g, "")); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM