[英]Return word before or after a string with newline characters
In short: I want to return the word right before or after a newline character in a string. 简而言之:我想在字符串中的换行符之前或之后返回该单词。 How would I accomplish that? 我该怎么做?
I want to return: 1,150 and Svendborg 我想返回: 1,150和斯文堡
This is my string: 这是我的字符串:
var newline = /\n/;
var str = "Specialzed Road Expert 2017\nkr.1,150 - Svendborg\n\nSpecialzed"
This will essentially match a whole line with a leading and trailing newline character with groups to match just the first and last "words". 这实际上将使整行与前导和尾随换行符匹配,并与各组匹配,以仅匹配第一个和最后一个“单词”。
var str = "Specialzed Road Expert 2017\\nkr.1,150 - Svendborg\\n\\nSpecialzed"; var matches = str.match(/\\n([^\\s]+).*?([^\\s]+)\\n/); console.log(matches);
Your words would be in matches[1]
and matches[2]
with matches[0]
being the whole line. 您的单词将在matches[1]
和matches[2]
,而matches[0]
是整行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.