Extracting information from a string - javascript

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

Related

jQuery Replace Second Space of Sentence

I want to replace second space occurrence of the sentence with a br.
I have tried this but it is deleting the rest.
var title = "My Title Needs Brace".split(" ").slice(0, 2).join(" ");
That will do the trick:
"My Title Needs Brace"
.split(' ')
.reduce(function (str, part, i) {
return str + (i === 2 ? '<br/>' : ' ') + part
});
// "My Title<br/>Needs Brace"
Let's break it and see how it works:
First, we take the string and split it. we'll use " " as our separator
"My Title Needs Brace".split(' ')
// ["My", "Title", "Needs", "Brace"]
Second, we'll use reduce to combine the array back into one string
["My", "Title", "Needs", "Brace"]
.reduce(function (str, part) { return str + ' ' + part }, '');
// "My Title Needs Brace"
Why reduce and not join?
The advantage of reduce over join is that it allows us to use a function, which will give us a fine-grained control over how we join back each part of the string
Now, all that left is to replace the 2nd space with <br/>,
for that, we'll use the 3rd argument of the reduce function, which stands for the index, and ask:
is this the 3rd part? use <br/>
otherwise, use " "
"My Title Needs Brace"
.split(' ')
.reduce(function (str, part, i) {
return str + (i === 2 ? '<br/>' : ' ') + part
});
// "My Title<br/>Needs Brace"
Note that this is the index of the string "part", not the spaces between them so the index is 2, not 1.
More about:
split
reduce
join
Try the following:
var title = "My Title Needs Brace".split(" ");
title.forEach(function(item, i, title){
if(i==1)
title[i] += "<br/>";
else
title[i] += ' ';
})
console.log(title.join(''));
I want to replace second space occurrence of the sentence with a br.
The simple way to do that is to add "<br/>" to the second element.
Here is the Code.
$(document).ready(function(){
var title = "My Title Needs Brace".split(" ");
title[1] = title[1]+"<br/>";
var newstr = title.join(" ");
$("#textd").html(newstr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="textd">
</div>
maybe that will help :
var title = "My Title Needs Brace".split(" ");
t1=title [0]; My
t2=title[1]; // Title
t3=title[2]; // Needs
t4=title[3]; // Brace
you can drew here anything :
var htmlString = '' + t1 +''+ t2 + '<br />' + t3 +''+ t4 + '';
$('Anywhere').append(htmlString);
You can do this without splitting the string:
var title = 'My Title Needs Brace'.replace(/( .*?) /, '$1<br>');
Here, String.replace takes a RegExp and a string as arguments. The regex matches everything from the first space up through the second space, keeping everything except the second space in a capturing group. The string replaces the entire match with the contents of the capturing group, followed by '<br>'. Since the capturing group doesn't include the second space, this effectively only replaces the second space.

How can I get the official Unicode name of a character in Javascript from either its string or numeric value? [duplicate]

I need to find out the names for Unicode characters when the user enters the number for it. An example would be to enter 0041 and get given "Latin Capital Letter A" as the result.
As far as I know, there isn't a standard way to do this. You could probably parse the UnicodeData.txt file to get this information.
Here should be what you're looking for. The first array is simply http://unicode.org/Public/UNIDATA/Index.txt with replacing newlines with |;
// this mess..
var unc = "A WITH ACUTE, LATIN CAPITAL LETTER 00C1| /*... really big array ...*/ |zwsp 200B";
var uncs=unc.split("|");
var final_a = [];
var final_s = "";
for each (var item in uncs) {
var _T=item.split("\t");
//final_a [_T[1]] = _T[0];
final_s += '"' + _T[1] + '"' + ' : ' + '"' + _T[0] + '",';
}
console.log (final_s);
// yields..
var unicode_lookup = { /*really big array*/ }
// which we can use like so ...
alert(unicode_lookup["1D01"]);
// AE, LATIN LETTER SMALL CAPITAL
SO doesn't preserve tabs so the first part may not work if you simply copy-paste it. You'll note that some characters are duplicates so you may want to do some cleanup.

replace 2 different sets of words in a string in javascript

so I need to replace 2 sets of words in a string; the title of the web page. However, I seem to be able to get one set of words to be removed.
The title is being created by wordpress, which is adding words at the start and end of the title which I don't want to be displayed (as I am calling the title, using PHP, to dynamically create a few bits of information on the page, which are subject to change)
The code I have so far is:
<script>
var str = document.title.replace(/ - CompanyName/i, '');
document.write(str);
</script>
However, I need something which is basically:
var str = document.title.replace(/ - CompanyName/i, '') && document.title.replace(/The /i, '');
This is because the title will produce itself like "The PAGETITLE - CompanyName"
Any ideas how to remove 2 sections of the same string?
Keep the title in a separate variable, re-assign the variable with the result of each replace, set the document title:
var title = "The PAGETITLE - CompanyName";
title = title.replace("The ", "");
title = title.replace(" - CompanyName", "");
document.title = title;
Or, if you like one-liners:
document.title = document.title.replace("The ", "").replace(" - CompanyName", "");
you can directly use like this
var title = "The PAGETITLE - CompanyName";
title.replace(/The(.*?)-[^-]*/,'$1')
var str = document.title.replace(/ - CompanyName/i, '').replace(/The
/i, '');
the replace function yields the new string, which you want to run through replace again.
You can chain functions in JQuery like this:
var str = document.title.replace(/ - CompanyName/i, '').replace(/The /i, '');

Probably a easy thing to do but how do I get javascript to only split if something is there, if not leave it as is

I'm using the following to get the title from a feed:
var posttitleGETcalendar = entry.title.$t;
var posttitleREMcalendar = posttitleGETcalendar.split("_");
var title = posttitleREMcalendar[0] + "<small>" + posttitleREMcalendar[1] + "</small>";
What I'm trying to do is split the titles I get after underscore and add the tag small to the part after it.
Works great when underscore is there, but it shows the word undefined after last word if underscore is not on title.
Is there a way to use
if underscore is on title, do
if not, leave it as is
Thank you.
if(/_/.test(posttitleGETcalendar)) {
// posttitleGETcalendar.indexOf('_') != -1 would do too
// do your stuff here
}
alternatively, you could use the replace method and skip the other auxilliar variables :
var title = entry.title.$t.replace('_', '<small>') + '</small>';
You can change the last line to:
var title = posttitleREMcalendar[0]
if ( posttitleREMcalendar.length > 1 )
{
title = title + "<small>" + posttitleREMcalendar[1] + "</small>";
}
This way you will only add the second part if it's present.

Regular expression for replacing string with javascript

I need help in writing regular expression:
part of my string is fixed and another part of its variable.
only if fixed AND variable string exist i need to alter the string other wise no.
Fixed string:example: AA.BBB.COM
Variable string (may or mayn't exist ): US, but if exist it will be always two letter string with any combination of letter.
In below string if I have variable two letter string exist I want to append “.new”
1 ) https://XY**.US**.AA.BBB.COM
Output: https:// XYZ12**.US.NEW**.AA.BBB.COM
2 ) https://XY.UK.AA.BBB.COM
Output: https:// XYZ12.UK.NEW.AA.BBB.COM
3) https://XY.AA.BBB.COM (no variable string so no change)
Output: https:// XY.AA.BBB.COM
Thanks for your help .
Raghav
Something like the following should get you started, there are other methods. Splitting and parsing might suit better depending on your real requirements:
var s = 'https://XY.US.AA.BBB.COM';
var t = 'https://XY.UK.AA.BBB.COM';
var u = 'https://XY.AA.BBB.COM';
var re = /(\.)(UK|US)(\.)/;
alert(
s.replace(re, '$1' + '$2' + '.NEW' + '$3') + '\n' +
t.replace(re, '$1' + '$2' + '.NEW' + '$3') + '\n' +
u.replace(re, '$1' + '$2' + '.NEW' + '$3')
);

Categories

Resources