小程序如何识别↵ ?
发布于 5 年前 作者 xiuying96 1155 次浏览 来自 官方Issues

引入wxs文件

{{tools.filter(item.description)}}

return text.replace(/↵/g, ‘/n’)

使用replace报错说  / 这个不能用,到底应该怎么识别 ↵这个换行

1 回复

不知道楼主问题解决了没有,我特地测试了一下,提供一下我的代码,亲测可用,大致的思路如下

  1. 使用encodeURIComponent对内容进行编码,得知↵编码为‘%0A‘

  2. 编码的内容把’%0A‘替换掉你需要的内容

  3. 把替换掉的内容使用decodeURIComponent解码之后返回

代码如下:

// utils.wxs


function replaceNewlineSymbol(text) {

  // 替换textarea的换行符,text为需要替换的内容
  var encodeText = encodeURIComponent(text);
  var regexp = getRegExp('%0A', 'g');
  var newText = encodeText.replace(regexp, '(Hello WORLD!)');
  return decodeURIComponent(newText);

}


module.exports = {

replaceNewlineSymbol: replaceNewlineSymbol

}



回到顶部