V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
andybest
V2EX  ›  问与答

如何将这个 Javascript 代码转换为 Java ?

  •  
  •   andybest · 2014-05-15 11:16:00 +08:00 · 4827 次点击
    这是一个创建于 3877 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个 Javascript 代码的函数,其中替换的正则是 JS 语法,如何转换为 Java 语法代码?

    function HTML2UBB(str) {
    str = str.replace(/\r/g,"");
    str = str.replace(/on(load|click|dbclick|mouseover|mousedown|mouseup)="[^"]+"/ig,"");
    str = str.replace(/<script[^>]*?>([\w\W]*?)<\/script>/ig,"");
    str = str.replace(/<p align=(.*)>(.*?)<\/p>/ig,"[$1]$2[\/$1]");
    str = str.replace(/<a[^>]+href="([^"]+)"[^>]*>(.*?)<\/a>/ig,"[url=$1]$2[/url]");

    str = str.replace(/<font[^>]+color=([^ >]+)[^>]*>(.*?)<\/font>/ig,"[color=$1]$2[/color]");
    str = str.replace(/<img[^>]+src="([^"]+)"[^>]*>/ig,"[img]$1[/img]");

    str = str.replace(/ /ig," ");
    str = str.replace(/\n/g," ");
    str = str.replace(/<\/p>/ig,"\n");
    str = str.replace(/<br[^>]*>/ig,"\n");
    str = str.replace(/<([\/]?)b>/ig,"[$1b]");
    str = str.replace(/<([\/]?)strong>/ig,"[$1b]");
    str = str.replace(/<([\/]?)u>/ig,"[$1u]");
    str = str.replace(/<([\/]?)i>/ig,"[$1i]");
    str = str.replace(/<([\/]?)em>/ig,"[$1i]");
    str = str.replace(/<([\/]?)center>/ig,"[$1center]");


    str = str.replace(/<([\/]?)pre>/ig, "[$1code]");
    str = str.replace(/<([\/]?)blockquote>/ig, "[$1quote]");

    //if the text mark as del, just remove it
    str = str.replace(/<del[^>]*>.*<\/del>/ig, "");

    str = str.replace(/&/g,"&");
    str = str.replace(/ +/g," ");
    str = str.replace(/\n /g,"\n");
    str = str.replace(/ \n/g,"\n");
    str = str.replace(/"/g,"\"");
    str = str.replace(/</g,"<");
    str = str.replace(/>/g,">");

    str = str.replace(/<[^>]*?>/g,"");
    str = str.replace(/\[url=([^\]]+)\]\n(\[img\]\1\[\/img\])\n\[\/url\]/g,"$2");

    return str;
    }
    5 条回复    2014-05-15 14:21:36 +08:00
    tonghuashuai
        1
    tonghuashuai  
       2014-05-15 11:25:05 +08:00
    扔到文本编辑器里查找替换就行吧,比如所有 replace 替换成 replaceAll
    andybest
        2
    andybest  
    OP
       2014-05-15 13:03:54 +08:00
    @tonghuashuai 谢谢,主要是正则部分,比如: str.replace(/<\/p>/ig,"\n"); 这个怎么替换成 Java 语法?
    jsonline
        3
    jsonline  
       2014-05-15 13:09:41 +08:00 via Android
    这代码写的好那啥
    PhilCai
        4
    PhilCai  
       2014-05-15 14:08:52 +08:00 via iPhone
    @andybest Java有专门正则的库,花几分钟看看就好了
    tonghuashuai
        5
    tonghuashuai  
       2014-05-15 14:21:36 +08:00   ❤️ 1
    @andybest java 中 replaceAll 的第一个参数正则表达式就是字符串,比如"<[^>]*?>",所以剩下的就好说了,举例:
    js 代码:
    str = str.replace(/<[^>]*?>/g,"");

    java 代码:
    String str = str.replaceAll("<[^>]*?>", "")

    将 /正则表达式/g 替换为 “正则表达式”,也就是将 / 和 /g 替换成 “

    注意默写符号的转义
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1187 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 18:17 · PVG 02:17 · LAX 10:17 · JFK 13:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.