繁体   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