How to get a Substring JAVASCRIPT? [duplicate] - javascript

This question already has answers here:
Get Substring between two characters using javascript
(24 answers)
Closed 4 years ago.
I need to iterate over strings that are inside an array, to get a sub-string in each string.
Substrings are between "()"
Something like this..
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
//return
//'cat','red','apple'
How I could do that?

You can do this using substring and lastIndexOf functions.
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
myArray.forEach(function(e){
console.log(e.substring(e.lastIndexOf("(") + 1, e.lastIndexOf(")")))
})

Related

Excluding Punctuation Marks in JavaScript include method [duplicate]

This question already has answers here:
Check if an array of strings contains a substring [duplicate]
(5 answers)
How do you search an array for a substring match?
(15 answers)
Closed 4 months ago.
Is there a better way of checking if a word exists in an array of strings without including Punctuation Marks?
what I have tried,
const sArray=['Lorem','Ipsum','typesetting-','industry.','Ipsum?','has' ]
console.log(sArray.toString().replaceAll(".","").includes("industry")) //true
console.log(sArray.includes("industry")) //false
Something like this?
sArray.some(str => str.includes('industry'))
If you do a lot of operations on the array, I suggest creating a new reference of the array after replacing, then dealing with the new array.
const sArray=['Lorem','Ipsum','typesetting-','industry.','Ipsum?','has' ]
const trasformedArray = sArray.map(i => i.replaceAll('.', ''));
console.log(trasformedArray.includes("industry"))

Sorting strings in javascript [duplicate]

This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Closed 3 years ago.
I would like to sort strings in javascript containing comma separated values in different e.g.
var Str = "8,0,2,10"
I want to sort it like below example form the last one to first one:
var NewStr = "10,2,0,8"
You can convert string to array using split() and reverse the array element using reverse() and then convert result to string again using join() like this:
var Str = '8,0,2,10';
var dif = Str.split(',').reverse().join(',');
console.log(dif);

convert string to an array using JQuery [duplicate]

This question already has answers here:
Convert string with commas to array
(18 answers)
Closed 4 years ago.
var string = "Chemistry,English,History";
Sir i want to convert it to an array like:
var subjects= ["Chemistry","English","History"];
using jQuery
No need of jquery. Just use .split() function to achieve this.
let string = 'Chemistry,English,History';
let arr = string.split(',');
console.log(arr)

How to locate and match substring of a string in JavaScript? [duplicate]

This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 5 years ago.
I need JavaScript algorithm that can match substring of a sting?
subStringFinder('abbcdabbbbbck', 'ab')
should return index 0
and
subStringFinder('abbcdabbbbbck', 'bck') should return index 10
Could you please tell me how to write this code?
--EDIT:
Thanks to #Jonathan.Brink I wrote that code and it did the trick:
function subStringFinder(str, subString) {
return str.indexOf(subString);
}
subStringFinder('abbcdabbbbbck', 'bck') // -> 10
You are looking for the indexOf function which is available via the built-in string type (as well as array).
Example:
var str = "abbcdabbbbbck";
var n = str.indexOf("bck");
// n is 9
Probably, rather than having a custom subStringFinder function it would be better to just use indexOf.

How to get numbers in a string separated with commas and save each of them in an Array in Javascript [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 5 years ago.
I have a string of ID's wherein they're being separated with commas.
For example, the string is:
"15,14,12,13"
How can I extract the numbers/id's from this string and save each of them in a JSON or array to be something like this
Array: {
15,
14,
12,
13
}
I don't know how it's done using regex or string manipulation. Please advice.
use split & map & parseInt methods.
var numbers="15,14,12,13";
var result=numbers.split(',').map(function(number){
return parseInt(number);
});
console.log('convert '+JSON.stringify(numbers)+" to array:"+JSON.stringify(result));
Use eval method
var numbers="15,14,12,13";
var result=eval("["+numbers+"]");
console.log('convert '+JSON.stringify(numbers)+" to array:"+JSON.stringify(result));

Categories