• 正则表达式替换文本中的换行


    今天工作中遇到了一个问题:使用textarea文本域来存放简单的文本信息,不需要有图片,但是每段文字的换行和缩进还是得需要的。

    为了达到这个目的,我的想法是:使用正则表达式匹配到所有文本信息中的换行符,然后将他们替换成html的<p>标签,存储的时候将替换后的文本信息存放到数据库,显示的时候直接从数据库取出数据显示即可。

    下面是我的实现代码:

    HTML:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <style type="text/css">
            .articlep { font-size:16px; color:#333; text-indent:32px; line-height:28px; margin-bottom:10px;}
        </style>
    <script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                $("#ceshi").click(function () {
                    var valstr = "<p class="articlep">" + $("#txtNewsContent").val().toString().replace(/(
    )*
    /g, "</p><p class="articlep">").replace(/s/g, " ") + "</p>";
                    alert(valstr);
                    $("#transferedtxt").html(valstr.toString().replace(/<p>/g, "").replace(/</p>/g, ""));
                });
            })
        </script>
    </head>
    <body>
        <form id="form1">
        <div>
            <label><span style=" color:Red;">*</span>新闻内容:</label>
            <textarea id="txtNewsContent" rows="10" cols="50">
            </textarea>
        </div>
        <input type="button" value="ceshi" id="ceshi"/>
        <br />
        <div id="transferedtxt">
        </div>
     
        </form>
    </body>
    </html>

    输入内容:

    日前,铁路部门再次迎来全系统统一加薪,根据岗位不同,每人每月上调薪酬幅度从300元至500元不等。
    这是盛光祖继任原铁道部部长和中国铁路总公司总经理后,铁路部门第4次统一加薪。

    输出内容:

  • 相关阅读:
    You can’t access this shared folder because your organization’s security policies block unauthenticated guest access
    iOS 程序退出之后
    给网站得标签头加上小图标
    ios外界蓝牙适配
    JavaScript&jquery判断下载地址是否可用,同时判断浏览器是否禁用弹窗
    GCD初级使用
    find与xargs
    sed命令详解
    shell变量详解
    date命令详解
  • 原文地址:https://www.cnblogs.com/stevenjson/p/4239543.html
Copyright © 2020-2023  润新知