在自定义文本之间获取内容

时间:2016-08-26 13:16:19

标签: javascript jquery html

我想在/*SumitStart*/*SumitStart*/

之间发短信

$(document).ready(function() {
  $("button").click(function() {
    var data1 = $("#myid").text();
    // alert(ss);
    var subStr = $("#myid").text().match("/*SumitStart*/(.*)/*SumitEND*/");
    alert(substr);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button">Get Substring</button>
<div id="myid">
  Hello My Div is large /*SumitStart*/ hello chirag good morning welcome /*SumitEND*/ And Now End My DIv sdfsdfsdfs
</div>

我的代码 HERE

4 个答案:

答案 0 :(得分:2)

var subStr = $("#myid").text().match(/\/\*SumitStart\*\/(.*)\/\*SumitEND\*\//)[0];
// result
/*SumitStart*/ hello chirag good morning welcome /*SumitEND*/

var subStr = $("#myid").text().match(/\/\*SumitStart\*\/(.*)\/\*SumitEND\*\//)[1];
// result
hello chirag good morning welcome

答案 1 :(得分:2)

您需要使用正则表达式

var subStr = $("#myid").text().match(/\/\*SumitStart\*\/.*\/\*SumitEND\*\//);

DEMO

答案 2 :(得分:1)

这有助于您:

$(document).ready(function(){

$("button").on("click",function(){

    $txt = $("div").text();

    $patt = /(\/\*SumitStart\*\/)\s*(.*)\s*(\/\*SumitEND\*\/)/gi;

    $res = $txt.match($patt,"$2");

    alert(RegExp.$2);
   })
})

最终代码:

<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    
    <button type="button">Get Substring</button>
    
    <div id="myid">
        Hello My Div is large
       /*SumitStart*/ hello chirag good morning welcome /*SumitEND*/  And Now End My DIv
       sdfsdfsdfs
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
        
    $(document).ready(function(){

        $("button").on("click",function(){

            $txt = $("div").text();

            $patt = /(\/\*SumitStart\*\/)\s*(.*)\s*(\/\*SumitEND\*\/)/gi;

            $res = $txt.match($patt,"$2");

            alert(RegExp.$2);
        })
    })
    
    </script>
</body>
</html>

答案 3 :(得分:1)

这有效:

subStr=data1.slice(data1.indexOf("/*SumitStart*/")+"/*SumitStart*/".length,data1.indexOf("/*SumitEND*/"));