Regex select word after specific word - javascript

How can I capture a word just after specific word in regex, I have to select everything between from - to and after to so there will be two capturing groups.
Example:
"From London to Saint Petersburg" I wanted to extract London Saint Petersburg from above string.
Im stuck with this code here, my current regex selecting to Saint Petersburg i wanted to get rid word from and to from the selection.
/(?=to)(.*)/i

You can capture the two groups you need and then use match to extract them:
s = "From London to Saint Petersburg"
console.log(
s.match(/From (.*?) to (.*)/).slice(1,3)
)

you can just use split() and use /From | to /, it will return an array containing split values
var str = "From London to Saint Petersburg";
var arr = str.split(/from | to /ig);
console.log(arr);

Here is sample code doing what you asks for:
<html>
<head>
</head>
<body>
</body>
<script>
var strIn = "From London to Saint Petersburg";
var regEx = /^From\s(.+?)\sto\s(.+?)$/;
var arrResult = regEx.exec(strIn);
var strOut = "Original:" + strIn + "<br>Result:<br>";
strOut += "1. " + arrResult[1] + "<br>";
strOut += "2. " + arrResult[2];
document.write(strOut);
</script>
</html>
Place this in a document. Open it with a browser. Here is how the result looks like:
Original:From London to Saint Petersburg
Result:
1. London
2. Saint Petersburg
Hope it helps!

Related

How to create an array out of a string and keep spaces & punctuation at the end of each word?

Take this string as example:
'The strong, fast and gullible cat ran over the street!'
I want to create a function that takes this string and creates the following array:
['The ','strong, ','fast ','and ','gullible ','cat ','ran ','over ','the ','street!']
Observe that I want to keep each puctuation and space after each word.
This regular expression will match what you want: /[^\s]+\s*/g;
EXAPMLE:
var str = 'The strong, fast and gullible cat ran over the street!';
var result = str.match(/[^\s]+\s*/g);
console.log(result);
You can split at word boundaries followed by a word character:
var str = 'The strong, fast and gullible cat ran over the street!';
console.log(str.split(/\b(?=\w)/));
As #Summoner commented (while I was modifying my code to do it), if we add some char(s) that we want to use as a delimiter, we can then split on that, rather than the spaces.
var s= 'The strong, fast and gullible cat ran over the street!';
s = s.replace(/\s+/g, " **");
var ary = s.split("**");
console.log(ary);
Gonna toss my solution into the ring as well. :)
var sTestVal = 'The strong, fast and gullible cat ran over the street!';
var regexPattern = /[^ ]+( |$)/g;
var aResult = sTestVal.match(regexPattern)
console.log(aResult);
The result is:
["The ", "strong, ", "fast ", "and ", "gullible ", "cat ", "ran ", "over ", "the ", "street!"]
The regex pattern breaks down like this:
[^ ]+ - matches one or more non-space characters, and then
( |$) - either a space or the end of the string
g - it will match all instances of the patternthe end

Regex to remove London except London City

I need to write a regex to remove London from the list except London City. Thanks in advance.
Input
London Heathrow, London Gatwick, London City, London Southend, London Stanstead
Output
Heathrow, Gatwick, London City, Southend, Stanstead
Use replace() method with negative look ahead assertion regex.
var str = 'London Heathrow, London Gatwick, London City, London Southend, London Stanstead';
console.log(
str.replace(/\bLondon\s(?!City\b)/gi, '')
)
Regex explanation here.
Your regex would be
/London City|London\s/g
Match London City first and then London and then in the matcher callback method replace based on matched value
try this as well
var input = "London Heathrow, London Gatwick, London City, London Southend, London Stanstead";
var output = input.replace(/London City|London\s/g, function(match){if (match == "London City") { return match } else { return "" }} );
console.log(output);
You can extend this regex to include other names as well (which you don't want to be replaced), for example
/London Heathrow|London City|London\s/g //would not replace London Heathrow and London City
You can make it more dynamic by using a regex constructor as
var itemsNotToBeReplaced = ["London Heathrow", "London City"];
var regex = new RegExp( itemsNotToBeReplaced.push("London\s").join("|"), "g" );
input.replace(regex , function(match){
if (itemsNotToBeReplaced.indexOf(match) != -1)
{
return match ;;
}
else
{
return "" ;
}
});

Extracting information from a string

So I got this string
G-Eazy - The track title (Mr. Awesome Remix) (Official Video)
Now I would like to extract information like the artist, song title, remix and ignore the information about the official video.
That means that I am just assuming that the first part is the artist's name followed by a space and minus sign and a space again. Then I would like to retrieve the content of the first brackets and ignore all brackets containing words like "official" and so on...
Is there any way to do that using regex?
The expression /^(.+?)\s+\-\s+(.+?)\s*\((.+?)\)/ seems to work as expected.
Example Here
var string = 'G-Eazy - The track title (Mr. Awesome Remix) (Official Video)';
var matches = string.match(/^(.+?)\s+\-\s+(.+?)\s*\((.+?)\)/);
document.querySelector('pre').textContent =
'Artist: ' + matches[1]
+ ' \nTitle: ' + matches[2]
+ '\nRemix: ' + matches[3];
<pre></pre>
Output:
Artist: G-Eazy
Title: The track title
Remix: Mr. Awesome Remix
If you are struggling with how to match the - that separates the artist from the track name without matching on the - in the artist name, then the trick is to match on something like ([^ ]| [^-])+ for the artist name. That will match "anything but a space, or a space not followed by a dash" repeatedly. Obviously we'd like to support spaces in the artist name as well.
For the whole expression, something like this should work:
var str = 'G-Eazy - The track title (Mr. Awesome Remix) (Official Video)'
var re = /^((?:[^ ]| [^- ])+) - ([^(]+)(?:\(([^)]+)[Rr]emix\))?/;
var m = str.match(re);
console.log('Artist: ' + m[1]);
console.log('Tack : ' + m[2]);
console.log('Remix : ' + m[3]);
Depending on whether all the data coming in is in an expected similar format or not, you could do it using the string tokenizing method .split().
var string = "G-Eazy - The track title (Mr. Awesome Remix) (Official Video)";
var artist = string.split('-')[0];
alert(artist); // "G-Eazy "
var title = string.split('-')[1].split('(Official')[0];
alert(title); // " The track title (Mr. Awesome Remix) ";
artist = artist.trim();
title = title.trim();
alert(artist + " - " + title); // "G-Eazy - The track title (Mr. Awesome Remix)"

regex or jquery to split string after certain character

I have a string like this:
Franciscan St. Francis Health - Indianapolis
I need to extract everything after '-' including the dash itself and output it in the second line..How do I extract everything before '-'?? Regex or jquery?
The string infront of '-' will be dynamic and could have varying number of letters...
Neither. I would just use the native .split() function for strings:
var myString = 'Franciscan St. Francis Health - Indianapolis';
var stringArray = myString.split('-');
//at this point stringArray equals: ['Franciscan St. Francis Health ', ' Indianapolis']
Once you've crated the stringArray variable, you can output the original string's pieces in whatever way you want, for example:
alert('-' + stringArray[1]); //alerts "- Indianapolis"
Edit
To address a commenter's follow-up question: "What if the string after the hyphen has another hyphen in it"?
In that case, you could do something like this:
var myString = 'Franciscan St. Francis Health - Indianapolis - IN';
var stringArray = myString.split('-');
//at this point stringArray equals: ['Franciscan St. Francis Health ', ' Indianapolis ', ' IN']
alert('-' + stringArray.slice(1).join('-')); //alerts "- Indianapolis - IN"
Both .slice() and .join() are native Array methods in JS, and join() is the opposite of the .split() method used earlier.
Regex or jquery?
False dichotomy. Use String.splitMDN
var tokens = 'Franciscan St. Francis Health - Indianapolis'.split('-');
var s = tokens.slice(1).join('-'); // account for '-'s in city name
alert('-' + s);
DEMO
join()MDN
slice()MDN
Probably no need for regex or jquery. This should do it:
var arr = 'Franciscan St. Francis Health - Wilkes-Barre'.split('-');
var firstLine = arr[0]
var secondLine = arr.slice(1).join('-');
Ideally, your data would be stored in two separate fields, so you don't have to worry about splitting strings for display.

Javascript Regex - Insert word (hit)

is it possible to insert the word that was found into the replace ?
$(function() {
content = 'hallo mein name ist peter und ich komme aus berlin. Und du?';
words = 'mein na,berlin'
words = words.replace(/,/,'\|');
words = words.replace(/\s/,'\\s');
regex = new RegExp(words,'gi');
content = content.replace(regex,'<strong>*insert here the word that was found*</strong>');
alert(''+content+'');
});
working example
http://www.jsfiddle.net/V9Euk/227/
Thanks in advance!
Peter
Try this:
content.replace(regex,'<strong>$&</strong>');
$& is replaced with the full match.
Working example: http://www.jsfiddle.net/V9Euk/228/
If you are more comfortable with it, you can add a group and replace it with $1 (this one will raise less questions):
words = words.replace(/,/g,'\|');
words = words.replace(/\s/g,'\\s');
words = '(' + words + ')';
regex = new RegExp(words, 'gi');
content = content.replace(regex,'<strong>$1</strong>');
Note that you probably want the g flag on these replaces, or you only change the first space and comma.
If you also want to avoid partial matching (so "mein na" doesn't capture), add \b:
words = '\\b(' + words + ')\\b';

Categories

Resources