是否有任何可能的方法来删除以特定关键字开头并以特定关键字结尾的键 javascript / node js
Is there any possible way to remove key which start with specific key words and end with specific key word in javascript / node js
我正在尝试替换字符串中存在的特定字母,但问题是我想替换多个字母,所以我必须手动编写所有替换代码
例如:-
let string= "<!-- ct:hold -->Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n\t<!-- ct:ol -->It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<!-- /ct:query>"
string.replace(/<!-- ct:text -->/g, "")
.replace(/<!-- ct:hold -->/g, "")
.replace(/<!-- ct:ol -->/g, "")
.replace(/<!-- ct:asset -->/g, "")
.replace(/<!-- ct:ques -->/g, "")
.replace(/<!-- ct:data -->/g, "")
let string= "<!-- ct:hold --><!--start-->Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n\t<!-- ct:ol -->It has survived not <!--end-->only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with <!--start-->desktop publishing software like<!--Fragrement--> Aldus PageMaker including versions of Lorem Ipsum.<!-- /ct:query -->";
string =string.replace(/<!--?\s+\/?ct:.*?-->/g,"");
console.log(string)
as here 你可以看到这是我正在替换的一些小值,但有
我要删除的大关键字
例如:-
string.replace(/<!-- ct:text -->/g, "")
.replace(/<!-- ct:hold -->/g, "")
.replace(/<!-- ct:ol -->/g, "")
.replace(/<!-- ct:asset -->/g, "")
.replace(/<!-- ct:ques -->/g, "")
.replace(/<!-- ct:data -->/g, "")
.replace(/<!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->/)
.replace(/<!-- ct:spacer {\"height\":120} -->/)
有没有可能我只能删除以 <!-- ct:
开头和以 -->
结尾的值,并且替换文本有多长都没有关系
string.replace(/^<\!-- ct:.*-->$/gi, "");
更新:
根据@MonkeyZeus 的建议,如果没有惰性评估,如果您的字符串中有多个 sub-strings 匹配,您可能会得到非常糟糕的结果,这样更安全:
string.replace(/<\!-- ct:.*? -->/gi, "");
这将匹配您的字符串中的所有单个子字符串:https://regex101.com/r/DxwRD6/1
您可以使用正则表达式通配符匹配 (.
) 和重复 1 次或多次修饰符 (+
) 来匹配 ct: 部分后注释中的任何字符。同样根据@MonkeyZeus 的评论,需要一个惰性量词 (?
),这样它就不会从第一个评论的开头一直匹配到字符串中最后一个评论的结尾:/<\!-- ct:.+? -->/g
.
或者,如果您不想使用正则表达式或替换,您可以使用 DOMParser,然后 return 不包含评论的文本内容
let string = "<!-- ct:hold -->Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n\t<!-- ct:ol -->It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<!-- /ct:query>"
const doc = new DOMParser().parseFromString(string, 'text/html')
console.log(doc.all[0].textContent)
除了已经与上述评论链接的 solutions/variants 之外,还有第三个变体,它在句法匹配方面不太严格 in/valid 评论,就像 OP 示例数据中的最后一个一样。
下面的代码显示了使用 ...
的不同结果
变体 2 ... /<!--?\s+\/?ct:.*?-->/g
变体 3 .../<!--?\s+\/?ct:.*?>/g
const sampleText = `
<!-- ct:hold -->Lorem Ipsum is simply dummy text of the printing and typesetting industry.
<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
<!-- ct:ol -->It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {"queryId":8,"query":{"perPage":"10","pages":0,"offset":0,"postType"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of
Lorem Ipsum.<!-- /ct:query>`;
// see ... [https://regex101.com/r/olNflD/2]
const regXComment2 = /<!--?\s+\/?ct:.*?-->/g;
// see ... [https://regex101.com/r/olNflD/3]
const regXComment3 = /<!--?\s+\/?ct:.*?>/g;
console.log(
sampleText.replace(regXComment2, '')
);
console.log(
sampleText.replace(regXComment3, '')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
我正在尝试替换字符串中存在的特定字母,但问题是我想替换多个字母,所以我必须手动编写所有替换代码
例如:-
let string= "<!-- ct:hold -->Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n\t<!-- ct:ol -->It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<!-- /ct:query>"
string.replace(/<!-- ct:text -->/g, "")
.replace(/<!-- ct:hold -->/g, "")
.replace(/<!-- ct:ol -->/g, "")
.replace(/<!-- ct:asset -->/g, "")
.replace(/<!-- ct:ques -->/g, "")
.replace(/<!-- ct:data -->/g, "")
let string= "<!-- ct:hold --><!--start-->Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n\t<!-- ct:ol -->It has survived not <!--end-->only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with <!--start-->desktop publishing software like<!--Fragrement--> Aldus PageMaker including versions of Lorem Ipsum.<!-- /ct:query -->";
string =string.replace(/<!--?\s+\/?ct:.*?-->/g,"");
console.log(string)
as here 你可以看到这是我正在替换的一些小值,但有 我要删除的大关键字
例如:-
string.replace(/<!-- ct:text -->/g, "")
.replace(/<!-- ct:hold -->/g, "")
.replace(/<!-- ct:ol -->/g, "")
.replace(/<!-- ct:asset -->/g, "")
.replace(/<!-- ct:ques -->/g, "")
.replace(/<!-- ct:data -->/g, "")
.replace(/<!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->/)
.replace(/<!-- ct:spacer {\"height\":120} -->/)
有没有可能我只能删除以 <!-- ct:
开头和以 -->
结尾的值,并且替换文本有多长都没有关系
string.replace(/^<\!-- ct:.*-->$/gi, "");
更新:
根据@MonkeyZeus 的建议,如果没有惰性评估,如果您的字符串中有多个 sub-strings 匹配,您可能会得到非常糟糕的结果,这样更安全:
string.replace(/<\!-- ct:.*? -->/gi, "");
这将匹配您的字符串中的所有单个子字符串:https://regex101.com/r/DxwRD6/1
您可以使用正则表达式通配符匹配 (.
) 和重复 1 次或多次修饰符 (+
) 来匹配 ct: 部分后注释中的任何字符。同样根据@MonkeyZeus 的评论,需要一个惰性量词 (?
),这样它就不会从第一个评论的开头一直匹配到字符串中最后一个评论的结尾:/<\!-- ct:.+? -->/g
.
或者,如果您不想使用正则表达式或替换,您可以使用 DOMParser,然后 return 不包含评论的文本内容
let string = "<!-- ct:hold -->Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n\t<!-- ct:ol -->It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {\"queryId\":8,\"query\":{\"perPage\":\"10\",\"pages\":0,\"offset\":0,\"postType\"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<!-- /ct:query>"
const doc = new DOMParser().parseFromString(string, 'text/html')
console.log(doc.all[0].textContent)
除了已经与上述评论链接的 solutions/variants 之外,还有第三个变体,它在句法匹配方面不太严格 in/valid 评论,就像 OP 示例数据中的最后一个一样。
下面的代码显示了使用 ...
的不同结果变体 2 ...
/<!--?\s+\/?ct:.*?-->/g
变体 3 ...
/<!--?\s+\/?ct:.*?>/g
const sampleText = `
<!-- ct:hold -->Lorem Ipsum is simply dummy text of the printing and typesetting industry.
<!-- /ct:hold -->Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
<!-- ct:ol -->It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged<!-- /ct:ol -->. <!-- ct:query {"queryId":8,"query":{"perPage":"10","pages":0,"offset":0,"postType"} -->It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of
Lorem Ipsum.<!-- /ct:query>`;
// see ... [https://regex101.com/r/olNflD/2]
const regXComment2 = /<!--?\s+\/?ct:.*?-->/g;
// see ... [https://regex101.com/r/olNflD/3]
const regXComment3 = /<!--?\s+\/?ct:.*?>/g;
console.log(
sampleText.replace(regXComment2, '')
);
console.log(
sampleText.replace(regXComment3, '')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }