Javascript 选择正斜杠前但空格后的第一个字符

Javascript 选择正斜杠前但空格后的第一个字符,javascript,regex,Javascript,Regex,我有以下字符串模式,我需要按照描述进行匹配 我只需要以下每个示例的第一个字符/数字。在“/”之前和任何空格之后: 12/5 <--match on 1 x23/4.5 match on x 234.5/7 match on 2 2 - 012.3/4 match on 0 澄清 我实际上是在使用带有js split的正则表达式,因此它是一个mpping函数,它接受每个字符串,并在匹配时将其拆分。因此,例如2-012.3/4将被拆分为[2-0,12.3/4]和12/5到1,[2/5]等等

我有以下字符串模式,我需要按照描述进行匹配

我只需要以下每个示例的第一个字符/数字。在“/”之前和任何空格之后:

12/5 <--match on 1
x23/4.5 match on x
234.5/7 match on 2
2 - 012.3/4 match on 0
澄清 我实际上是在使用带有js split的正则表达式,因此它是一个mpping函数,它接受每个字符串,并在匹配时将其拆分。因此,例如
2-012.3/4
将被拆分为
[2-0,12.3/4]
12/5到1,[2/5]
等等

请参见此处的示例(使用非工作正则表达式):


如果您希望能够扫描整个文档:

/(?<=(^|\s))\S(?=\S*\/)/g

/(?此正则表达式中的第一个组与您要求的字符匹配:

([^\s])[^\s]*/
您也可以使用:

[^\s]+/

然后使用匹配的第一个字符(或者您可能还需要其他字符)。

尝试以下正则表达式:

(?<=^|\s)[a-zA-Z0-9](?=[^\s]*[/])
这是输出:

text: '12/5'
  matched '1' at offset 0 in text.

text: 'x23/4.5'
  matched 'x' at offset 0 in text.

text: '234.5/7'
  matched '2' at offset 0 in text.

text: '2 - 012.3/4'
  matched '0' at offset 4 in text.

text: '12/5, x23/4.5, 234.5/7, 2 - 012.3/4'
  matched '1' at offset 0 in text.
  matched 'x' at offset 6 in text.
  matched '2' at offset 15 in text.
  matched '0' at offset 28 in text.

你的正则表达式在
“12/5”
上有效,但在
“12/5”
上不起作用-我想这是OP要求的?但他说“在任何空格之后”这个正则表达式只在每个字符串的开头有空格而没有空格的情况下起作用。是“12/5”或“2-012.3/4”@s.Schenk使它也起作用了…我想OP要求“在任何空格之后”但是,这是匹配字符串的一个实例并提取该字符,还是继续获取整个文档的匹配?0上的
2-012.3/4
如何匹配?您是指最后一个空格和“/”之间的匹配字符吗?我的意思是仅匹配0。在这种情况下,我需要在0上拆分。它始终是“/”之前部分的第一个字符但有时也可能是这样:2-012.3/4
text: '12/5'
  matched '1' at offset 0 in text.

text: 'x23/4.5'
  matched 'x' at offset 0 in text.

text: '234.5/7'
  matched '2' at offset 0 in text.

text: '2 - 012.3/4'
  matched '0' at offset 4 in text.

text: '12/5, x23/4.5, 234.5/7, 2 - 012.3/4'
  matched '1' at offset 0 in text.
  matched 'x' at offset 6 in text.
  matched '2' at offset 15 in text.
  matched '0' at offset 28 in text.