简体   繁体   English

如何从JavaScript中的API字符串确定大小可变的多个子字符串

[英]How to determine multiple substrings of varying size from an API string in javascript

I have an API call that as part of the object returns something like this: 我有一个API调用,作为对象的一部分返回了如下内容:

"aaps":"1.50U\/h 0.36U(0.18|0.18) -1.07 0g"

I would like to pull two substrings out of this but the size of the substrings will vary. 我想从中拉出两个子串,但是子串的大小会有所不同。 For example, I would like one variable to be the substring of the number that precedes the second "U". 例如,我希望一个变量是第二个 “ U”之前的数字的子字符串。 But this value will be anywhere between 0.00 and 100.99. 但是此值将在0.00到100.99之间。 So I can't just count on using indexof() . 所以我不能只指望使用indexof()

As well I would like another variable to be the substring of the number that precedes the "g". 同样,我希望另一个变量是“ g”之前数字的子字符串。 This variable will have a value of anywhere between 0 and 250 so the number of characters of the substring will vary. 此变量的值将介于0到250之间,因此子字符串的字符数将有所不同。

JavaScript's exec function is great for this. JavaScript的exec函数对此非常有用。

var string = '1.50U\/h 0.36U(0.18|0.18) -1.07 0g';
var regex = /.+U.+(\d+\.\d{2})U.+(\d+)g/;
var match = regex.exec(string);
if (match) {
  console.log('Matches: ' + match[1] + ', ' + match[2]);
  // Matches: 0.36, 0
}

The regular expression captures the two numbers you need, and the exec function extracts them into an array. 正则表达式捕获您需要的两个数字,然后exec函数将它们提取到数组中。 Here's what the regular expression means: 正则表达式的含义如下:

.+                         | Match one or more consecutive characters (any)
  U                        | Match the letter "U"
   .+                      | Match one or more consecutive characters (any)
     (                     | Capture the following:
      \d+                  |     One or more consecutive digits
         \.                |     The character "."
           \d{2}           |     Exactly 2 consecutive digits
                )          | End capture
                 U         | Match the letter "U"
                  .+       | Match one or more consecutive characters (any)
                    (\d+)  | Capture one or more consecutive digits
                         g | Match the letter "g"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript - 如何检查一个字符串是否包含多个子字符串 - Javascript - How to check if a string contains multiple substrings JavaScript字符串中多个子字符串的计数 - Count of multiple substrings in JavaScript string 替换字符串中的多个子字符串-Javascript - replacing multiple substrings in a string - Javascript JavaScript - 从优化的角度来看,应该如何用不同的字符串替换字符串中的多个子字符串? - JavaScript - From an optimization standpoint how should one replace multiple substrings in a string with different strings? Javascript - 如何在一个条件下检查字符串是否包含多个子字符串 - Javascript - How to check if a string contains multiple substrings in one condition 如何在JavaScript中多个子串中的一个子串上拆分字符串? - How to split a string on the first occurrence of one of multiple substrings in JavaScript? 如何从javascript中的字符串获取子字符串? - How can I obtain substrings from a string in javascript? 如何检查字符串是否包含 JavaScript 中子字符串数组中的文本? - How to check if a string contains text from an array of substrings in JavaScript? 如何创建Regex模式以从React中的字符串中删除多个子字符串? - How to create Regex pattern to delete multiple substrings from a string in React? Javascript在一个字符串中获取多个子字符串 - Javascript Get Multiple Substrings Within One String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM