[英]Regexp javascript
我需要編輯我ref = '521590819123';
來只得到ref = '521590819123';
如何 ?
<script type="text/javascript">
var ref = "http://www.site.ru/profile/521590819123";
if( ref.indexOf('profile') >= 0 ) {
ref = String(ref).substr(ref.indexOf('profile'));
}
alert(ref);
</script>
只需將 indexof 更改為 LastIndexOf
ref = String(ref).substr(ref.lastIndexOf('/')+1);
使用lastIndexOf
:
<script type="text/javascript">
var ref = "http://www.site.ru/profile/521590819123";
if( ref.indexOf('profile') >= 0 ) {
ref = ref.substr(ref.lastIndexOf('/') + 1);
}
alert(ref);
</script>
見這里: http : //jsfiddle.net/CFeVV/
如果要通過正則表達式解決此任務,請使用:
ref = ref.replace(/^.*\/(\d+)$/g, "$1")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.