2016-06-01 5 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>

Смежные вопросы