String function in react [duplicate] - javascript

This question already has answers here:
regex for "mentions"
(2 answers)
Closed 1 year ago.
I want to get the word after the # from the string like. "hello coders am #ayub a beginner in react". want to get that (ayub) from the string after #. any help.

You can find the String.prototype.indexOf and String.prototype.substring
let data = "hello coders am #ayub a beginner in react";
let startIndex = data.indexOf('#');
let endIndex = data.indexOf(' ', (startIndex + 1));
let result = data.substring(startIndex+1, endIndex);
console.log(result);

Firstly, you do not need a React specific function to do that. You can write a simple javascript function like so -
function getName(str) {
return str.match(/#[a-z]+/)[0].substring(1);
}
console.log(getName("hello coders am #ayub a beginner in react"));
This is a pretty fast solution

Related

How are +variable+ and ${variable} different in javascript? [duplicate]

This question already has answers here:
ES6 template literals vs. concatenated strings
(4 answers)
Closed 1 year ago.
I'm start coding.
While I was watching a lecture, I saw code like this.
var coworkers = ['go', 'hello', 'hi', 'doit'];
<script type="text/javascript">
var i = 0;
while(i < coworkers.length){
document.write('<li>'+coworkers[i]+'</li>');
i = i + 1;
}
But But when I searched, Said to use ${variable} in JavaScript. and It didn't work.
How are +variable+ and ${variable} different in javascript? Thanks :)
Those placeholders, like ${variable}, can only be used in template literals, which are always enclosed in backticks(the key right below you escape key).
If it still doesn’t work, maybe you’re using an older browser?

Add double quotes to arraylist of string in javascript [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 3 years ago.
I am new to stack overflow learning javascript and programming.
I have a problem that i am stuck while learning and thinking any help on this question will be
useful for me thanks and the question is:
Example i have a variable a in the code below and i want to convert it to an array in javascript
var a = ["baby,cat,dog"]
i wanted it to be
a = ["baby","cat","dog"].
Use String.prototype.split() as below
var a = ["baby,cat,dog"];
a = a[0].split(',');
console.log(a);
You could take the array and map the splitted values and get a flat array back.
var array = ["baby,cat,dog"];
result = array.flatMap(s => s.split(','));
console.log(result);

Please tell me what this code means [duplicate]

This question already has answers here:
What is the result of this javascript?
(3 answers)
Closed 5 years ago.
can you tell me what this code does as I need to understand it for a coding assessment. I only just started learning JavaScript
function getAttackString() {
var foo = "d323b8b34";
var bar = "x334q3j98";
return "The code is: "+(foo.substr(3,foo.length-6))+(bar.substr(2));
}
Thanks for your help
Start by looking up what .length and .substr are.
For example, what will foo.length give you? A good way to find out would be:
var x = "abc123";
console.log(x.length);
console.log is a command to print information to your web browser console, very helpful when doing debugging work.

JavaScript operator similar to SQL “like” [duplicate]

This question already has answers here:
Emulating SQL LIKE in JavaScript
(10 answers)
Closed 8 years ago.
I have this function with jquery, but I need it to work like operator "like"
Help me.
Thank you very much for your help
works correctly if I look the whole word
example
var vsearch = "home"
var array = _.filter(objeto, function(product){
return product.filtro ==vsearch;
var vsearch=`H`
var array = _.filter(objeto, function(product){
return product.filtro ==vsearch;
No looks for containing the H
Thanks.
Use indexOf, which returns -1 if a string is not found, to see if the product contains the letter you're searching for.
var vsearch=`H`;
var array = _.filter(objeto, function(product){
return product.filtro.indexOf(vsearch) > -1;
}
You could try something like this:
var stringToSearch = "The full string to search goes here";
var searchTermContains = stringToSearch.search("H");
This will return a -1 if no match is found.

Check if a string contains a specific number in JavaScript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: string contains
I want to check if a string contains a specific number in JavaScript, how can I rewrite the statement s contain d in below code?
var s = '11/14/2012';
var d = '14';
if (s contain d) {
//...
}
My case is similar to Check if a string contains a certain number, but how can I implement this action in JavaScript?
Thanks
The easier way is to use index Of.
if(s.indexOf(d) > -1)
var s = '11/14/2012';
var d = '14';
if (s.match(d)) {
alert('Found');
}​

Categories

Resources