简体   繁体   English

Javascript中的正则表达式(没有jQuery)?

[英]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. 我是Java语言的新手,最近我想使用正则表达式以便从url获取数字并将其存储为字符串形式的var和数字形式的另一个var。 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. 例如,我想从下面的网页(不是应计页面)中获取数字55,并将其存储在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. 请我需要帮助,但不需要jQuery,因为它对我来说没有多大意义。

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