0

我已经知道如何从这样的 url 中提取一个数字asdf/asdasd.gsdfg-sdfg-asdasd=34234,但是如果我的 URL 看起来像这样/blaah/sdfggsdfgdsfg-13998.html.html?editcode=814228c1aa 并且我需要提取的数字是 this ->13998怎么办?

4

4 回答 4

0

You should do this way, getting the url from the browser and then match the number:

var number = window.location.href.toString().match(/-(\d+)\./)[1];
于 2013-07-01T14:10:19.217 回答
0

假设您熟悉RegExp您可以轻松提取您的号码。请参阅此处以获取有效的jsbin

希望能帮助到你。

于 2013-07-01T14:23:03.710 回答
0
"/blaah/sdfggsdfgdsfg-13998.html.html?editcode=814228c1aa".match(/-(\d+)\./)[1]
//                                                                ^^^^^^^^ regex
于 2013-07-01T14:04:09.363 回答
0

这真的取决于情况。如果你得到遵循特定的、相当简单的模式的常规数据。您可以安全地使用 split() / substring() 等函数。

但是,使用正则表达式 (regexp) 会更充分,它为您提供更大的灵活性和更安全的解决方案。

于 2013-07-01T14:04:19.807 回答