【问题标题】:Javascript/Jquery Get part of stringJavascript/Jquery 获取字符串的一部分
【发布时间】:2014-12-15 13:10:22
【问题描述】:

是否有可能得到这样的第一个字符串?

loadTabContent('90', this, 'tabTabel', null);
loadTabContent('101', this, 'tabTabel', null);

对于第一个,我希望一个变量返回“90”,如果是最后一个,我希望它返回“101”。我该怎么做呢?数字的长度是可变的,但总是从同一个地方开始。

【问题讨论】:

  • 看起来不像字符串?无论如何,str.match(/\d+/) 应该得到第一个数字!
  • 我看到我的字符串确实不正确。你知道我是否有一个 onclick 属性,比如onclick="loadTabContent('90', this, 'tabTabel', null);",我是否可以将loadTabContent('90', this, 'tabTabel', null); 放在一个字符串中?我试过var tabString = $("#" + tabID).attr("onclick");,但是javascript把所有东西都放在了字符串中,而不仅仅是onclick属性的字符串值

标签: javascript jquery string


【解决方案1】:
<script type="text/javascript">
 var re = /'\d+(.*?)'/;
 var sourcestring = "source string to match with pattern";
 var results = [];
 var i = 0;
 for (var matches = re.exec(sourcestring); matches != null; matches = re.exec(sourcestring)) {
 results[i] = matches;
 for (var j=0; j<matches.length; j++) {
  alert("results["+i+"]["+j+"] = " + results[i][j]);
  }
 i++;
}

$matches Array:
  (
  [0] => Array
    (
        [0] => '90'
        [1] => '101'
    )

[1] => Array
    (
        [0] => 
        [1] => 
    )

 )

【讨论】:

    猜你喜欢
    • 2011-04-12
    • 2016-06-13
    • 2023-03-24
    • 1970-01-01
    • 2011-09-04
    • 2015-02-02
    相关资源
    最近更新 更多