今天工作中有一个需求
例1:
building aaa(belong)(aaa 与 ‘(’ 之间没有空格)
building aaa (belong)(aaa 与 ‘(’ 之间有空格)
都替换为
building aaa (belong)(aaa 与 ‘(’ 之间有空格)
解决:
var str="building aaa(belong)" 或"building aaa (belong)"
str=str.replace(/aaa(.)/g,"aaa $1")
//$1 就表示前面正则中的 ‘(.)’
//无论 ‘( )’中代表的是什么,$1都会表示为‘( )’ 中的这个字符
例2:
---we are famliy.(没有空格)↵--(没有空格)---It is ___(没有空格)(ssd) cool 或
---we are famliy. (有空格)↵--(有空格)---It is ___ (有空格)(ssd) cool
其中 ↵--代表的是换行符 ___ 代表input
解决:
var str = "---we are famliy.↵-----It is so cool "
或 "---we are famliy. ↵-- ---It is so cool "
str = str.replace(/(.)\n(.)/g,"$1 huanhang $1")
/*
我的需求有些复杂,需要用空格拆分字符串成数组,循环显示出来(实际更复杂) ,然后该换行的换行,该替换成input的就替换,这里只说换行,循环时判断显示 等于huanhang 就显示<br />就行了
*/