I wanna change the url text into a words, but have no idea to do that. Please help me.
Here's what I wanna do, example:
some-text-url.html
into
some text url
Use the split method:
var url = "some-text-url.html";
url = url.replace(".html", ""); // remove html
var words = url.split("-");
// words is now an array of the keywords
var str = "some-text-url.html";
str = str.split('.')[0].split('-').join(' ');
.split() on the . gives an Array of:
[
"some-text-url",
"html"
]
[0] gives the first string in the Array "some-text-url"
.split() on the - gives an Array of:
[
"some",
"text",
"url"
]
And .join() passing a string with a single space gives the final result:
"some text url"
Or here's another way to avoid creating an Array with .split():
var str = "some-text-url.html";
str = str.replace(/-|\.html$/g," ");
Giving you "some text url ".
Notice the space on the end. If you don't want that, add .slice(-1) after the .replace().
Related
For example, I have:
var str = "Hello
World"
I'm expecting an array like that : array["Hello", "World"]
I looked for a method that does that but nothing, I tried to make a loop but I don't know on what I should base my loop? From my knowledge there's not a .length property for the amount of lines in a string...
Use the split function:
var str = `Hello
World`;
var splittedArray = str.split(/\r?\n/);
console.log(splittedArray)
First thing is that the input string is not valid. It should be enclosed by backtick not with a quotes and then you can replace the new line break with the space and then split it to convert into an array.
Live Demo :
var str = `Hello
World`;
const replacedStr = str.replace(/\n/g, " ")
console.log(replacedStr.split(' '));
How to replace "[ by [ and "] by ]in a string below:
I replace "] by ] by this code:
var str = "[{"propertyid":10000005,"title":"country"}]"
var newstr = str .replace(/\]"/g, ']')
But I do not know how to replace "[ by [?
you cannot define a string that way it will give error
if you want the string that way you can put the string between ``
var str = `"[{"propertyid":10000005,"title":"country"}]"`
but in what scenario you want to use it that way
also lets say you received string from server or file representing array of objects
like this var str = '[{"propertyid":10000005,"title":"country"}]'
you can use arr=JSON.parse(str) to to convert it to a array in memory
var str = "https://cdn.fbsbx.com/v/t59.2708-21/68856895_411975049700005_8580443955521388544_n.xls/test.xls?_nc_cat=106&_nc_oc=AQmcm2PVCUFFyUJDJgLs3ZYM4Dg12PX1Wv48Fm0LJ8-Qi8duxOpEVrD2uFgrD9e1pDOXcLpJmbtjbveAm12xczd2&_nc_ht=cdn.fbsbx.com&oh=18eab18ae1d1cf2a95084bba0a002163&oe=5D8F8124";
var n = str.substring(str.indexOf("\\.") +1 , str.indexOf("?_nc_cat="));
I have this string but my output is :
https://cdn.fbsbx.com/v/t59.2708-21/68856895_411975049700005_8580443955521388544_n.xls/test.xls
How can i get only this .xls?
My other string is :
https://scontent.xx.fbcdn.net/v/t1.15752-9/70629455_2730574856953299_3640328874664919040_n.png?_nc_cat=100&_nc_oc=AQm0m5jryh7zAzyj2R-w7ke0DKQgHM7aYaVkkRjPYDUQ6g-FUAWqVwhnr7qxqISkWMdiNhtp7e8gYMA6gss58poN&_nc_ad=z-m&_nc_cid=0&_nc_zor=9&_nc_ht=scontent.xx&oh=1cbb98fb9484bd3f26b6058808cca889&oe=5E36459B
but again im not getting just word "png" im getting from start link.
I'd use a regular expression, and match word characters while looking ahead for ?:
const getFileType = str => str.match(/\w+(?=\?)/)[0];
console.log(getFileType("https://cdn.fbsbx.com/v/t59.2708-21/68856895_411975049700005_8580443955521388544_n.xls/test.xls?_nc_cat=106&_nc_oc=AQmcm2PVCUFFyUJDJgLs3ZYM4Dg12PX1Wv48Fm0LJ8-Qi8duxOpEVrD2uFgrD9e1pDOXcLpJmbtjbveAm12xczd2&_nc_ht=cdn.fbsbx.com&oh=18eab18ae1d1cf2a95084bba0a002163&oe=5D8F8124"));
console.log(getFileType('https://scontent.xx.fbcdn.net/v/t1.15752-9/70629455_2730574856953299_3640328874664919040_n.png?_nc_cat=100&_nc_oc=AQm0m5jryh7zAzyj2R-w7ke0DKQgHM7aYaVkkRjPYDUQ6g-FUAWqVwhnr7qxqISkWMdiNhtp7e8gYMA6gss58poN&_nc_ad=z-m&_nc_cid=0&_nc_zor=9&_nc_ht=scontent.xx&oh=1cbb98fb9484bd3f26b6058808cca889&oe=5E36459B'));
Simply, Just you have to use the LastIndexof() in the right way to achieve this
str = "https://scontent.xx.fbcdn.net/v/t1.15752-9/70629455_2730574856953299_3640328874664919040_n.png?_nc_cat=100&_nc_oc=AQm0m5jryh7zAzyj2R-w7ke0DKQgHM7aYaVkkRjPYDUQ6g-FUAWqVwhnr7qxqISkWMdiNhtp7e8gYMA6gss58poN&_nc_ad=z-m&_nc_cid=0&_nc_zor=9&_nc_ht=scontent.xx&oh=1cbb98fb9484bd3f26b6058808cca889&oe=5E36459B";
url = str.substring(0,str.indexOf("?"));
ext = str.substring(url.lastIndexOf(".")+1, url.length);
The same will work for any other string as well.
Since it seems you're trying to get the last value from pathname which is preceded by . from URL, so you can use URL API
Simply parse the URL with URL api, take the patname value and split on . and take the last element from splitted array.
var str = "https://cdn.fbsbx.com/v/t59.2708-21/68856895_411975049700005_8580443955521388544_n.xls/test.xls?_nc_cat=106&_nc_oc=AQmcm2PVCUFFyUJDJgLs3ZYM4Dg12PX1Wv48Fm0LJ8-Qi8duxOpEVrD2uFgrD9e1pDOXcLpJmbtjbveAm12xczd2&_nc_ht=cdn.fbsbx.com&oh=18eab18ae1d1cf2a95084bba0a002163&oe=5D8F8124";
let valueExtractor = (str) =>{
let urlParse = new URL(str)
return urlParse.pathname.split('.').pop()
}
console.log(valueExtractor(str))
console.log(valueExtractor("https://cdn.fbsbx.com/v/t59.2708-21/68856895_411975049700005_8580443955521388544_n.xls/test.png?_nc_cat=106&_nc_oc=AQmcm2PVCUFFyUJDJgLs3ZYM4Dg12PX1Wv48Fm0LJ8-Qi8duxOpEVrD2uFgrD9e1pDOXcLpJmbtjbveAm12xczd2&_nc_ht=cdn.fbsbx.com&oh=18eab18ae1d1cf2a95084bba0a002163&oe=5D8F8124"))
Hi I have a text something like
"Welcome back ##Firstname ##Lastname. You Last accessed on ##Date"
My objective is to replace these tokens with actual values.
So what i did was
var str = "Welcome back ##Firstname ##Lastname. You Last accessed on ##Date;
var data = str.split('#');
My idea was - once i do this, my data will have an array of values something like
["Welcome back", "#FirstName" , "#LastName", "You Last accessed on" , "#Date"]
Once i have this, i can easily replace the tokens because i will know which one are properties and which one are static string. But fool i am since JS has other ideas.
it instead split it as :
["Welcome back ", "", "Firstname ", "", "Lastname. You Last accessed on ", "", "Date"]
What am i doing wrong? or what is the best way to replace tokens in a string?
I looked here. Did not like the approach much. Not a fan of curly brackets. would like to do it the "#" way - Since it will be easy for Content authors
Another regex option, split on /#(#\w+)[^\w#]+/, captures the name part while throwing off the first #, assuming the name identifiers are always made up of word characters:
var str = "Welcome back ##Firstname ##Lastname. You Last accessed on ##Date;"
var data = str.split(/#(#\w+)[^\w#]+/);
console.log(data.filter(s => s !== ""));
You can split "#" characters which are followed by "#" characters
var str = "Welcome back ##Firstname ##Lastname. You Last accessed on ##Date";
var res = str.split(/#(?=#)/);
console.log(res);
you can replace the '##' by some character followed by '#' like ',#' and then replace on that new character.
var str = "Welcome back ##Firstname ##Lastname. You Last accessed on ##Date";
var data = str.replace(/##/g, ',#').split(',');
console.log(data);
As your delemiters end with a space, may split by space:
.split(" ")
And then iterate and replace all words beginning with ##
var replaceBy={
lastname:"Doe",
name:"John"
}
var result= input.split(" ").map(function(word){
if(word[0]=="#" && word[1]=="#"){
return replaceBy[word.substr(2)] || "error";
}
return word:
}).join(" ");
However, it might be easier to suround your identifiers with delemiters e.g.:
Hi ##lastname##!
So you can do
.split("##")
And every second element is automatically an identifier.
Do this:
data = str.replace("##Firstname", "Robert").replace("##Lastname","Polson").replace('##Date','yesterday') ;
I have got of array symbols as shown below
var sourcesymbols = ["ERT", "UBL" , "AMAZING"];
I am getting the following news title from rss feed
you experts are amazing
How to check if the content present in the rssfeedstring is present under the sourcesymbols array or not ??
For example rssfeedstring has word amazing and it is also present under sourcesymbols
please let me know how to achive this .
I have tried to convert the rssfeedstring to uppercase then i am not sure how to use the indexOf on the string .
rssfeedstring = rssfeedstring.toUpperCase();
please let em know if there is any better approach also for doing this as the array will have 2000 symbols
http://jsfiddle.net/955pfz01/3/
You can use regex.
Steps:
Convert the array to string with join using |(OR in regex) as glue
Use \b-word boundary to match exact words
Use i flag on regex to match irrespective of the case. So, don't have to change the case of string.
Escape the slashes as using RegExp constructor requires string to be passed and \ in string is used as escape following character.
test can be used on regex to check if the string passes the regex.
var sourcesymbols = ["ERT", "UBL", "AMAZING"];
var mystr = 'you experts are amazing';
var regex = new RegExp("\\b(" + sourcesymbols.join('|') + ")\\b", 'i'); // /\b(ERT|UBL|AMAZING)\b/i
alert(regex.test(mystr));
You can also use some
Convert the string to array by using split with \s+. This will split the string by any(spaces, tabs, etc) one or more space character
Use some on splitted array
Convert the string to uppercase for comparing
Check if the element is present in array using indexOf
var mystr = 'you experts are amazing';
var sourcesymbols = ["ERT", "UBL", "AMAZING"];
var present = mystr.toUpperCase().split(/\s+/).some(function(e) {
return sourcesymbols.indexOf(e) > -1;
});
alert(present);
Try using Array.prototype.map() , Array.prototype.indexOf() to return matched text, index of matched text within sourcesymbols
var sourcesymbols = ["ERT", "UBL" , "AMAZING"];
var mystr = 'you experts are amazing';
var res = mystr.split(" ").map(function(val, index) {
var str = val.toUpperCase(), i = sourcesymbols.indexOf(str);
return i !== -1 ? [val, i] : null
}).filter(Boolean);
console.log(res)
jsfiddle http://jsfiddle.net/955pfz01/6/