2016-06-01 101 views
1

我有一個字符串如下:如何從Javascript字符串中刪除特定的「序列」?

Well, here we are.^2000 Ain't much to look at, is it?^2000 Came here on a Wednesday night once.^1000 It was actually pretty crowded.^1000 But on a Tuesday evening .^300 .^300 .^1000 I guess it's just you^1000 and me.^3000 Heh. 

現在我不知道我怎麼會刪除隨後的^所以它最終將輸出以下,

Well, here we are. Ain't much to look at, is it? Came here on a Wednesday night once. It was actually pretty crowded. But on a Tuesday evening . . . I guess it's just you and me. Heh. 
+1

你想使用[Regex](http://www.regular-expressions.info/tutorial.html)。 – Jhecht

回答

2

使用此:

var res = str.replace(new RegExp("(\\^\\d+)","gm"), ""); 

哪裏str是字符串,正則表達式匹配^<number>和更換字符串""

1

畢竟^和數字正如我在評論中所說的,你想使用一種叫做Regex的東西。

$(document).ready(function() { 
 

 
    var html = $('#start').html(); 
 

 
    var output = html.replace(/(\^\d{2,4})/g, ''); 
 

 
    $('#results').html(output); 
 

 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="start"> 
 
    Well, here we are.^2000 Ain't much to look at, is it?^2000 Came here on a Wednesday night once.^1000 It was actually pretty crowded.^1000 But on a Tuesday evening .^300 .^300 .^1000 I guess it's just you^1000 and me.^3000 Heh. 
 
</div> 
 
<div id="results"> 
 
</div>

相關問題