[英]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.