简体   繁体   中英

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". I need "is new" as my output.

I have the below Regex which is working when i try in regex101 site.

 (?<=\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 :

 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+ - one or more non-whitespaces
  • \s+ - one or more whitespaces
  • (\S+\s\S+) - Group 1: one or more non-whitespaces, one or more whitespaces, and one or more non-whitespaces

See the JavaScript demo:

 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):

 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] );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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