简体   繁体   中英

Regular expression in Javascript (without jQuery)?

I am new to Javascript and recently I wanted to use regular expression in order to get a number from url and store it into a var as string and another var as digit. For example I want to get the number 55 from the below webpage (which is not an accrual page) and I want to store it in a var.

I tried this but it is not working

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

Please I need help but not with jQuery because it does not make a lot of sense to me.

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

(假定匹配;如果可能不匹配,请在应用数组下标之前检查结果。)

Try this

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

match is an array, match[0] contains the matched expression from your script, match[1] is the number (the 1st parenthesis), and so on

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM