[英]Move matched text to the end of string using Javascript regular expression
hello bye!
goodbye again.
我希望将上面文本中出现的任何bye
移动到最后。 我不确定它是否可以通过 Javascript 中的正则表达式完成。
我希望输出文本是:
hello !
good again.
bye
bye
您可以使用替换和加入
\\n
替换了字符串和数组元素 let str = `hello bye! goodbye again.` let replacer = (str) => { let temp = [] let strTemp = str.replace(/bye/g, match => { temp.push(match) return '' }) return [strTemp, ...temp].join('\\n') } console.log(replacer(str))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.