简体   繁体   English

正则表达式提取第二和第三个字符串

[英]Regex to extract 2nd and 3rd string

I am trying to extract the 2nd and 3rd string from a sentence with space being always be the delimiter.我试图从一个句子中提取第二个和第三个字符串,空格总是作为分隔符。 E,g - my sentence is "Regex is new for me". E,g - 我的句子是“正则表达式对我来说是新的”。 I need "is new" as my output.我需要“是新的”作为我的输出。

I have the below Regex which is working when i try in regex101 site.当我在 regex101 站点中尝试时,我有以下正则表达式。

 (?<=\s)(.*?)(?=\s) (?<=\s)(.*?)(?=\s)

I am thinking if this can be achieved in a better way using a different expression.我在想是否可以使用不同的表达方式以更好的方式实现这一点。

The easiest approach here is probably just to use split :这里最简单的方法可能只是使用split

 var input = "Regex is new for me"; var parts = input.split(" "); console.log("2nd term: " + parts[1]); console.log("3rd term: " + parts[2]); var combined = parts[1] + " " + parts[2]; console.log("combined: " + combined);

You can match a sequence of non-space characters, slice the results, and re-join them.您可以匹配一系列非空格字符,对结果进行切片,然后重新加入它们。

 console.log('Regex is new for me'.match(/\S+/g) .slice(1, 3).join(' ')); /** * @param {String} str Sequence of words to split * @param {int} start Starting index * @param {int} end Ending index */ const subwords = (str, start, end) => str.match(/\S+/g) .slice(start, end).join(' '); console.log(subwords('Regex is new for me', 1, 3)); /** * @param {String} str Sequence of words to split * @param {int} count Number of words to keep * @param {[int=0]} offset Initial word offset */ const subwords2 = (str, count, offset=0) => str.match(/\S+/g) .slice(offset, offset + count).join(' '); console.log(subwords2('Regex is new for me', 2, 1));

This is how I would do it.我就是这样做的。 I get all words and the join 2nd and 3rd.我得到了所有的话,并加入了第二和第三。

 var str = document.querySelector("#Phrase").textContent, words = str.match(/(\w+)/g), result = []; result.push(words[1]); result.push(words[2]); document.querySelector("#ResultPhrase").textContent = result.join(" ");
 <p id="Phrase">Regex is new for me</p> <p id="ResultPhrase"></p>

You could use following regex.您可以使用以下正则表达式。

(?<=\s)\w+\s\w+

Note : We do not use any options of regex.注意:我们不使用任何正则表达式选项。 We get first match.我们得到第一场比赛。

 var string = "Regex is new for me"; var result = string.match(/(?<=\s)\w+\s\w+/); console.log(result);

You can also use你也可以使用

const result = (string.match(/^\s*\S+\s+(\S+\s\S+)/) || ['', ''])[1];

See the regex demo .请参阅正则表达式演示

Details :详情

  • ^ - start of string ^ - 字符串的开头
  • \s* - zero or more whitespaces \s* - 零个或多个空格
  • \S+ - one or more non-whitespaces \S+ - 一个或多个非空格
  • \s+ - one or more whitespaces \s+ - 一个或多个空格
  • (\S+\s\S+) - Group 1: one or more non-whitespaces, one or more whitespaces, and one or more non-whitespaces (\S+\s\S+) - 第 1 组:一个或多个非空格、一个或多个空格以及一个或多个非空格

See the JavaScript demo:请参阅 JavaScript 演示:

 const string = "Regex is new for me"; const result = (string.match(/^\s*\S+\s+(\S+\s\S+)/) || ['', ''])[1]; console.log(result); // => is new

If you must make sure you only match alphanumeric chars (not just any non-whitespace chars), you can replace \S with [a-zA-Z0-9] (for ASCII only support) or [\p{N}\p{L}] (with u flag, for the whole Unicode support):如果您必须确保只匹配字母数字字符(而不仅仅是任何非空白字符),您可以将\S替换为[a-zA-Z0-9] (仅支持 ASCII)或[\p{N}\p{L}] (带有u标志,用于整个 Unicode 支持):

 console.log( ("Regex is new for me".match(/^\s*[a-zA-Z0-9]+\s+([a-zA-Z0-9]+\s[a-zA-Z0-9]+)/) || ['', ''])[1] );

Or或者

 console.log( ("Regex is new for me".match(/^\s*[\p{L}\p{N}]+\s+([\p{L}\p{N}]+\s[\p{L}\p{N}]+)/u) || ['', ''])[1] );

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

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