【问题标题】:Extract a word in a dynamic string提取动态字符串中的单词
【发布时间】:2016-12-13 15:34:57
【问题描述】:

在本例中,我想提取字符串中的给定价格。

const string = 'I am a long sentence! My price is $5,000 on a discount!';
const price = string.substring(string.lastIndexOf("$")+1,string.lastIndexOf(" "));
document.write(price);

上面的方法不起作用,因为它选择了空间的最后一个索引(" "

如何在价格之后立即获得空间?


编辑

字符串可能是其他任何东西!我无法控制它,我只想提取价格。以防我没有说清楚。

【问题讨论】:

  • 寻找空格,后跟“on”。
  • 我不能这样做,因为句子不会总是在价格后面包含“开”字。

标签: javascript regex string substring


【解决方案1】:

正则表达式并不总是正确的工具,但对于这种情况,它似乎是一个显而易见的选择。您真正想要的是与$ 匹配的所有内容,后跟数字和逗号的混合,以任何其他字符结尾。

const re = /\$[\d,]+/
const string = 'I am a long sentence! My price is $5,000 on a discount!';
const prices = re.exec(string);
console.log(prices[0]);

您可能希望扩展该模式,例如,也匹配 . -- /\$[\d,.]+(将捕获“$5,000.25”)或者更加宽松,除了空格之外的任何内容:/\$[^ ]+/(将捕获"$tonsofmoney")。

【讨论】:

  • 我接受了您的回答,尽管我没有具体提及。动态语句可能在价格后包含句点.。我想我必须动手学习一些正则表达式!
【解决方案2】:

你可以试试:

const string = 'I am a long sentence! My price is $5,000 on a   discount!';
const price = (string.split("$")[1].split(" ")[0]);
document.write(price);

【讨论】:

    【解决方案3】:
    const string = 'I am a long sentence! My price is $5,000 on a discount!';
        const price =string.split('$')[1]//text after $
                 .split(' ')[0];// text after $ and before space
        document.write(price);
    

    const string = 'I am a long sentence! My price is $5,000 on a discount!';
    const price =string.split('$')[1].split(' ')[0];
    document.write(price);

    const string = 'I am a long sentence! My price is $5,000 on a discount!';
        const price =string.match(/\$[0-9]*,[0-9]*/);
    //match $XXX,XXX in the string and estract it
        document.write(price);

    【讨论】:

    • 你能简单解释一下上面的代码是如何工作的吗?谢谢编辑:第二种方法行不通!正如我所提到的,给我的字符串是动态的,这意味着每次都不会相同。
    【解决方案4】:

    请试试这个。我将它分成多行以提高可读性。

    const string = 'I am a long sentence! My price is $5,000 on a discount!';
    var dollarPosition = string.lastIndexOf("$");
    var nextSpace = string.indexOf(" ", dollarPosition+1);
    const price = string.substring(dollarPosition+1,nextSpace);
    document.write(price);

    【讨论】:

    • 这种方式非常适合这种情况,感谢您将其分解为步骤,现在似乎很清楚了!
    【解决方案5】:

    const string = 'I am a long sentence! My price is $5,000 on a discount!' +
      'Another price: $120.';
    
    var m = string.match(/\$[\d,]+/g);
    var i, price;
    if (m) {
      for (i = 0; i < m.length; i++) {
        price = m[i].replace('$', '');
        console.log(price);
      }
    }

    【讨论】:

      【解决方案6】:

      你可以使用string.split()

      const string = 'I am a long sentence! My price is $5,000 on a discount!';
      const price = (string.split('$')[1]).split(' ')[0];
      document.write(price);

      这会在 $ 的第一个索引处拆分字符串并获取 $ 之后的部分。
      比它在第一次出现空格时拆分字符串并占据空格之前的部分。

      请注意,这要求字符串中只有 1 个 $ 符号。

      【讨论】:

        猜你喜欢
        • 2021-11-01
        • 2020-07-03
        • 1970-01-01
        • 1970-01-01
        • 2011-11-29
        • 1970-01-01
        • 2018-04-18
        • 2020-02-24
        • 1970-01-01
        相关资源
        最近更新 更多