String 如何编写一个JS函数来转换带有散列和单词的字符串,并输出由相应的头标记包围的单词

String 如何编写一个JS函数来转换带有散列和单词的字符串,并输出由相应的头标记包围的单词,string,String,例如: Input: "## Header" --> Output: "<h2>Header<h2>" Input: "### Header" --> Output: "<h3>Header<h3>" Input: "#### Header" --> Output: "<h4>Header<h4>

例如:

Input: "## Header" --> Output: "<h2>Header<h2>" 
Input: "### Header" --> Output: "<h3>Header<h3>"
Input: "#### Header" --> Output: "<h4>Header<h4>"
...up to 6 hashes for <h6>
Input:##Header-->Output:“Header”
输入:“标题”-->输出:“标题”
输入:“标题”-->输出:“标题”
…最多6个哈希值,用于
谢谢

这是函数

功能转换器(str){
让count=str.match(/#/g)和&str.match(/#/g).length;
返回计数?“+str.replace(/#/g)”).trim()+”:str
}
控制台日志(变压器(“#标题”);
console.log(transformer(“##头”);
log(transformer(“####头”);

log(transformer(“######头”)请尝试以下示例

函数getTag(字符串){
常量[hashes,word]=string.split(“”);
开关(hashes.length){
案例1:
返回`${word}`;
案例2:
返回`${word}`;
案例3:
返回`${word}`;
案例4:
返回`${word}`;
案例5:
返回`${word}`;
案例6:
返回`${word}`;
}
}
log(getTag(“##头”);
log(getTag(“####头”);
log(getTag(“头”)
函数转换(字符串){
const text=/\w+/.exec(字符串);
常量i=/\\+/.exec(字符串)[0]。长度;
返回`${text}`
}
log(转换(“##头”);
log(转换(“####头”);
log(转换(“######头”)您可以使用正则表达式

函数genertag(输入){
var n=input.match(/^\#+/g)[0]。长度
var el=input.match(/\w+/g)[0]
返回`${el}`
}
log(generatag(“####头”))
console.log(generatag(“##头”))
let input=“##标题”
让separatedStringArray=input.split(“”);
log(separatedStringArray);
if(separatedStringArray!=未定义&&separatedStringArray!=空&&separatedStringArray.length==2){
设totalhases=separatedStringArray[0]。长度;
console.log(totalhases);
log(“+separatedStringArray[1]+”);
}
You can count the number of hashes in the input. sample code.