简体   繁体   English

JavaScript RegExp三部分

[英]JavaScript RegExp Three Parts

I need a help with regular expressions in JavaScript (Sorry, I'm still trying to learn it.). 我需要JavaScript正则表达式的帮助(很抱歉,我仍在尝试学习它。)。

I have the string: value1 value2 (value3 value4) -value5 value6 (Can have infinite values ​​in each of three parts.) 我有字符串: value1 value2 (value3 value4) -value5 value6 (三个部分中的每个都可以有无穷大的值。)

And I need to separate: 我需要分开:

var1 = value1 + ' ' + value2 ...
var2 = value3 + ' ' + value4 ...
var3 = value5 + ' ' + value6 ...

Can you help me with this? 你能帮我吗? Thanks! 谢谢!

Splitting may be an idea, if the formatting of the string stays like the given format: 如果字符串的格式保持为给定格式,则拆分可能是一个主意:

'value1 value2 (value3 value4) -value5 value6'.split(/\s+\(|\)\s+\-/);
//=> ["value1 value2", "value3 value4", "value5 value6"]

Since the original string already contains those space delimiters, try: 由于原始字符串已经包含那些空格分隔符,请尝试:

/^(.*) \((.*)\) -(.*)$/.exec(str);

It matches the string "anything (anything) -anything" . 它与字符串"anything (anything) -anything"匹配。

It returns an array of which arr[1] , arr[2] and arr[3] contain the three results. 它返回一个数组,其中arr[1]arr[2]arr[3]包含三个结果。

Note though that the values are not allowed to have the special characters in them (like ( or - with a space prepended) otherwise the format is not really definable. 请注意,尽管不允许在值中包含特殊字符(例如(-带有空格),否则不能真正定义格式。

var str1 = "value1 value2 (value3 value4) -value5 value6";

//var parts = str1.match(/^([^(]+)\(\s?([^)]+)\)\s?(.+)$/);
var parts = str1.match(/^([^(]+)\(\s?([^)]+)\)\s?-(.+)$/);

var var1 = parts[1];
var var2 = parts[2];
var var3 = parts[3];

console.log(var1);
console.log(var2);
console.log(var3);

I would go about finding each part first, then split the values in each for more efficiency. 我将首先查找每个部分,然后将每个部分中的值分开以提高效率。 Then I would stick them together using a single space. 然后,我将它们用一个空格粘在一起。 I used a lot of \\s* 'es because I'm assuming there might be multiple spaces (such as those in the commented line). 我使用了很多\\s* ,因为我假设可能存在多个空格(例如注释行中的空格)。

var str = 'value1 value2 (value3 value4) -value5 value6';
//var str = '  value1    value2 (   value3 value4)   -value5 value6 ';

var pairs = str.match(/^\s*(.+?)\s*\(\s*(.+?)\s*\)\s*(.+?)\s*$/);

values1 = pairs[1].split(/\s+/);
values2 = pairs[2].split(/\s+/);
values3 = pairs[3].split(/\s+/);

console.log('values1:',values1,'-->',values1.join(' '));
console.log('values2:',values2,'-->',values2.join(' '));
console.log('values3:',values3,'-->',values3.join(' '));

Explanation of RegEx RegEx的说明

  • ^ beginning of string ^字符串开头
  • \\s* possible empty spaces \\s*可能的空白
  • (.+?) a character group (at least 1 character) (.+?)一个字符组(至少1个字符)
  • \\s* possible empty spaces \\s*可能的空白
  • \\( opening parantheses \\(开头括号
  • \\s* possible empty spaces \\s*可能的空白
  • (.+?) a character group (at least 1 character) (.+?)一个字符组(至少1个字符)
  • \\s* possible empty spaces \\s*可能的空白
  • \\) closing parantheses \\)结束括号
  • \\s* possible empty spaces \\s*可能的空白
  • (.+?) a character group (at least 1 character) (.+?)一个字符组(至少1个字符)
  • \\s* possible empty spaces \\s*可能的空白
  • $ end of string $字符串结尾

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM