收藏
回答

小程序如何识别↵ ?

引入wxs文件

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

return text.replace(/↵/g, '/n')

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

回答关注问题邀请回答
收藏

2 个回答

  • russ😈
    russ😈
    2019-10-28

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

    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

    }




    2019-10-28
    有用 1
    回复 1
    • [奸笑]长忆秋🌻
      [奸笑]长忆秋🌻
      2020-10-23
      完美解决了 使用textarea显示带换行的文本时, 禁用编辑无效的bug 相当牛逼 ,
      2020-10-23
      回复
  • [奸笑]长忆秋🌻
    [奸笑]长忆秋🌻
    2020-10-23

    可以可以的,这个好用,

    2020-10-23
    有用
    回复
登录 后发表内容