如何从Javascript字符串中删除特定的“序列”?

时间:2016-06-01 03:23:38

标签: javascript regex

我有一个字符串如下:

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.

2 个答案:

答案 0 :(得分:2)

使用此:

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

str是字符串,正则表达式匹配^<number>,替换字符串为""

答案 1 :(得分:1)

正如我在评论中所说,你想要使用名为Regex的东西。

&#13;
&#13;
$(document).ready(function() {

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

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

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


});
&#13;
<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>
&#13;
&#13;
&#13;