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

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.

Related

Remove a certain character from a string in an array [duplicate]

This question already has answers here:
Remove a character at a certain position in a string - javascript [duplicate]
(8 answers)
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 18 days ago.
I'm trying to do a calculation with time and need to remove the ":" which splits hours and minutes.
My array currently holds a string value of "12:04"
I created a for loop to iterate through the second array string by length, check for a :, then remove that character and log the new output. However, my logic is not working as intended. If you can, please let me know what I did wrong so I can fix my issue.
for (let i = 0; i < content[2].length; i++) {
if (content[2].charAt(i) === ":"){
content[2].slice(i);
console.log(content[2])
}
}
If you are sure that ":" will appear only once, then keep it simple
content[2] = content[2].replace(":", "");
Full code:
const result = content.map(str => str.replace(":", ""))
I think this works well:
content.split(':').join('')
Here is the output I got from the console:
"12:04".split(':').join('')
'1204' // Output

How to get a Substring JAVASCRIPT? [duplicate]

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

Javascript get string occurrences [duplicate]

This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Regex created via new RegExp(myString) not working (backslashes)
(1 answer)
Closed 4 years ago.
I'm trying to return how many times the string s.t() was found in a string, but I can't get the correct regex for this...
For example
var string = 'function(test) {s.t(); s.t(dsabf); s.t();}'
var re = new RegExp('s\.t\(\)', "g");
return re;
should return an array of 2 elements ['s.t()', 's.t()'] but instead it has 3 elements ['s.t', 's.t', 's.t']
I've also tried with ^s\t\(\)$ but this returns no match...
How can I fix my regex in order to make this work as expected?

getting a number using regular expressions [duplicate]

This question already has answers here:
Regular expression for extracting a number
(4 answers)
Closed 6 years ago.
I have the following string:
PR-1333|testtt
I want to get the number 1333 using regular expressions in javascript. How can I do that?
This will look for numbers in your text.
var text = "PR-1333|testtt";
var number = text.match(/\d+/g); // this returns an array of all that it found.
console.log(number[0]); // 1333

How to find location of third "_" in a string - JavaScript [duplicate]

This question already has answers here:
Finding the nth occurrence of a character in a string in javascript
(6 answers)
Closed 9 years ago.
I have a JavaScript variable which contains something like "fld_34_46_name". I need to be able to find the location of the THIRD _. The numbers, and name are not always the same (so, the string might also look like "fld_545425_9075_different name_test").
Is this possible? How could I do it?
Use the indexOf method three times:
var i = s.indexOf('_');
i = s.indexOf('_', i + 1);
i = s.indexOf('_', i + 1);
Note: If the string might contain fewer than three underscores, you would need to check for -1 after each time.

Categories