searching a string in array by partial of it [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I wanna find a string inside an array by some part of its name(at least more than 3characters), Like:
Buffalo is an index of array and I want to search buf or buff, buffa, buffal or even the hole word(buffalo) and it return Buffalo.
No matter if its case-sensitive or NOT
here is my array:
const carsName =
[
"Landstalker",
"Bravura",
"Buffalo",
"Linerunner",
"Perrenial",
"Sentinel",
"Dumper",
"Firetruck",
"Trashmaster",
"Stretch",
"Manana",
"Infernus"
]
I wrote it in PAWN once and I'm gonna write it in JS (for my Alt V server) but I'm stuck at this point, is it possible to do?
and at the end, sorry for my English

Case insensitive partial match:
const carsName = [
"Landstalker",
"Bravura",
"Buffalo",
"Linerunner",
"Perrenial",
"Sentinel",
"Dumper",
"Firetruck",
"Trashmaster",
"Stretch",
"Manana",
"Infernus",
];
const partial = 'buf';
const foundName = carsName.find(
name => name.toLowerCase().includes(partial.toLowerCase())
);
console.log(foundName);

Related

Capture matching text format in a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
The community is reviewing whether to reopen this question as of 12 months ago.
Improve this question
I am trying to write a regex to extract the items in the text below that start with the # and ends with )
const bodyOfText = "#[DataStructures](topic_DataStructures) is one #[Algorithms](topic_Algorithms) branch that could #[Make or Mar](topic_Make or Mar)";
So basically, will want an array that looks like:
["#[DataStructures](topic_DataStructures)", "#[Algorithms](topic_Algorithms)", "#[Make or Mar](topic_Make or Mar)"]
Using string match() we can try:
var bodyOfText = "#[DataStructures](topic_DataStructures) is one #[Algorithms](topic_Algorithms) branch that could #[Make or Mar](topic_Make or Mar)";
var matches = bodyOfText.match(/#\[.*?\]\(.*?\)/g);
console.log(matches);

How to Filter an array of strings based on first word in the string in javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
With the following array of strings in javascript, is there a way to use the .filter to filter by the first word?
['Desk_One', 'Desk_Two', 'Desk_Three', 'Chair_One', 'Chair_Two']
For example to .filter(x = 'Desk_*')
['Desk_One', 'Desk_Two', 'Desk_Three']
Maybe this needs to be a regex
You can use startsWith() function like this:
let array = ['Desk_One', 'Desk_Two', 'Desk_Three', 'Chair_One', 'Chair_Two'];
let result = array.filter((item)=>item.startsWith(array[0].split('_')[0]));
console.log(result)

Regex to replace 'null-null-1234' to 'xxx-xx-1234' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to replace string
'null-null-1234' to '***-**-1234'
const p = 'null-null-1234';
const regex = /null/gi;
console.log(p.replace(regex, '***'));
Output => "***-***-1234" NOT AS EXPECTED
p.replace(/null-null/, '***-**')
I think what you actually want to do is to make a distinction between first and second values.
Hence, you probably need 2 regexes.
p.replace(/^null/, '***').replace(/-null-/, '-**-')
The first one will replace the null in first position (if any) by 3 stars, and the second replace will replace a null in the middle (if any) by 2 stars.

Comparing a string array with object array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to compare two arrays
array1=["123',"456"];
array2=[{id":"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name"baak"},{id:"456",name"sxs"}];
my objective is to extract the objects from array2 whose ids match the values in array1.
Can someone help me with the optimal solution?
First of all your second array (array2) is syntactically invalid.
You can try with Array.prototype.filter() and Array.prototype.includes()
var array1=["123","456"];
var array2 = [{id:"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name:"baak"},{id:"456",name:"sxs"}];
var res = array2.filter(i => array1.includes(i.id));
console.log(res);

How to split a text into an array of sentences (separated by full stop, comma, question mark etc) using Javascript/Jquery? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to split the content of an article into its sentences.
For example, Splitting
"Hi, My Name is Mark. I am going to the store, Do you need anything?"
to an array like
["Hi," , "My Name is Mark." , "I am going to the store," , "Do you need anything?"]
Could you help with the code using split() ?
let str = "Hi, My Name is Mark. I am going to the store, Do you need anything?",
strArray = str.split(/[,.]/),
spliterArray = str.match(/[,.]/g),
newArray = [];
for (let [key, val] of strArray.entries()) {
let splitVal = spliterArray[key] ? spliterArray[key] : "";
newArray.push(val.trim() + splitVal);
}
console.log(newArray);

Categories