Charachter使用Eclipse识别JavaScript

时间:2016-12-21 11:18:35

标签: javascript eclipse

我有一个包含参数名称的长字符串。 例如“最大行程距离 MAX_TRVL_DIST 为4米。”

我想从字符串中提取 MAX_TRVL_DIST ,任何人都可以请求帮助。

谢谢。

2 个答案:

答案 0 :(得分:0)

 var s =  "The max travel distance MAX_TRVL_DIST is 4 meter.";
 s.split(' ').forEach(function(str){

     if (str.indexOf('_') > -1) {
       console.log(str);
      }

    });

答案 1 :(得分:0)

正则表达式找到句子,然后简单地替换不需要的填充:

//Load string
var str = 'The max travel distance MAX_TRVL_DIST is 4 meter';
//Find matches
var matches = str.match(/(MAX_TRVL_DIST is \w*)/ig, '');
//If have matches
if (matches.length > 0) {
  //Find match value
  var value = matches[0].replace('MAX_TRVL_DIST is ', '');
  //Log the value of "MAX_TRVL_DIST"
  console.log(value);
}