簡體   English   中英

Javascript中的正則表達式(沒有jQuery)?

[英]Regular expression in Javascript (without jQuery)?

我是Java語言的新手,最近我想使用正則表達式以便從url獲取數字並將其存儲為字符串形式的var和數字形式的另一個var。 例如,我想從下面的網頁(不是應計頁面)中獲取數字55,並將其存儲在var中。

我嘗試了這個,但是沒有用

https://www.google.com/55.html
    url.replace(/(\d+)(\.html)$/, function(str, p1, p2) {
                    return((Number(p1) + 1) + p2);

請我需要幫助,但不需要jQuery,因為它對我來說沒有多大意義。

var numPortion = url.match(/(\d+)\.html/)[1]

(假定匹配;如果可能不匹配,請在應用數組下標之前檢查結果。)

嘗試這個

var a="https://www.google.com/55.html";
var match = a.match(/(\d+)(\.html)/);

match是一個數組,match [0]包含腳本中的匹配表達式,match [1]是數字(第一個括號),依此類推

var url = 'http://www.google.com/55.html';
var yournumber = /(\d+)(\.html)$/.exec(url);
yournumber = yournumber && yournumber[1];  // <-- shortcut for using if else

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM