【问题标题】:How do I get value of two digits in string如何获取字符串中两位数的值
【发布时间】:2016-06-11 11:22:38
【问题描述】:

假设,我有以下字符串(原始字符串中没有换行符):

Lorem ipsum dolor sit amet, [b]consectetur adipisici elit[/b],
sed eiusmod tempor incidunt ut labore et [fontSize=12]dolore [/fontSize=12] 
magna aliqua` in my textarea.

我想提取字体大小(在本例中为 12),欢迎使用任何解决方案(包括正则表达式)。

【问题讨论】:

  • @bobblebubble:我的错误,已更正。
  • @jan ty 格式化我的代码

标签: javascript jquery regex string font-size


【解决方案1】:

this rather simple pattern:

\[fontSize=(\d+)\]
# will look for fontSize= in square brackets 
# and captures the digits found

在 JavaScript 代码中,这将是:

var regex = /\[fontSize=(\d+)\]/;
var str = "Lorem ipsum dolor sit amet, [b]consectetur adipisici elit[/b], sed eiusmod tempor incidunt ut labore et [fontSize=12]dolore [/fontSize=12] magna aliqua";
var match = regex.exec(str);
var fontSize = match[1];
print(fontSize);

a demo on ideone.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 2017-04-27
    • 1970-01-01
    • 2011-02-05
    • 2021-10-27
    • 1970-01-01
    相关资源
    最近更新 更多