This question already has answers here:
How to remove part of a string?
(7 answers)
Closed 10 months ago.
I have a variable with a string, let's say this one, which I then display on a page on the site:
let value = "qwe asd — bensound summer";
document.getElementById("text").innerHTML = value;
And I want to remove its second part when displaying this line on the page bensound summer along with a dash —.
And in order to receive only the first part, which is before the dash, when displayed on the page, in the form: qwe asd.
I read about str.split() but didn't find anything like it and didn't quite understand how it all works.
Use the split method
let value = "qwe asd - bensound summer";
value = value.split('-')[0] //
document.getElementById("text").innerHTML = value;
You can use it like this:
let value = "qwe asd — bensound summer";
let splittedString = value.split("-");
document.getElementById("text").innerHTML = splittedString[0];
Hope, it helps!!
Related
This question already has answers here:
Use dynamic (variable) string as regex pattern in JavaScript
(8 answers)
Closed 5 years ago.
I have a string "platform:17.01.02" and I want to search for "17.01.02" which I'm giving through a variable.
a = "platform:17.01.02"
b = "17.01.02" (has to taken from user)
a.search(/b/)
The above statement search for "b" and not for variable value "b". Can any one help how can i search that?
Try this:
var a = "platform:17.01.02";
var b = "17.01.02";
console.log(a.search(new RegExp(b))); //9
If you just want to know if a contains b, use:
a.indexOf(b) >= 0
This question already has answers here:
Getting parts of a URL with JavaScript
(5 answers)
Closed 5 years ago.
url:
http://xxxxxx.com/video/view/12345
Can I take 12345 in the url using javascript?
Please help me
Use RegExp, Array#match and negative lookahead.
var str = 'http://xxxxxx.com/video/view/12345';
console.log(str.match(/(?!view\/)\d+/)[0]);
You can also try this if you're sure that it'll always be in last:
var num = location.pathname.split('/').pop(); // "12345"
and further: parseInt(num);
You can parse your URL with the following code. Then just get the last part.
var url = 'http://xxxxxx.com/video/view/12345';
var url_parts = url.replace(/\/\s*$/,'').split('/');
console.log(url_parts[url_parts.length - 1]); // last part
This question already has answers here:
Regex to extract substring, returning 2 results for some reason
(5 answers)
Closed 1 year ago.
When parsing this
<title>I have a cat</title> <div class="chat_message_wrapper chat_message_right">
with
var re_title = new RegExp("<title>(.*?)</title>");
var content = xmlhttp.responseText;
// var title = re_title.exec(content);
var title = content.match(re_title);
// title = strip(title);
alert(title);
for some reason, I get this in an alert:
<title>I have a cat</title>,I have a cat
How can I only get the “I have a cat” part?
The match method returns an array with first the complete match, and in the other elements the capture groups. Since you have one capture group, you need to get the value from the array at index 1:
title[1]
This question already has answers here:
How to get the first element of an array?
(35 answers)
Closed 8 years ago.
I am trying to get the first word out of the variable var solution = [cow, pig]
I have tried everything from strings to arrays and I can't get it. Please help.
As per the comments
solution[0]
Will return the first item in the array.
solution[1]
would be the second, or undefined if the array was:
var solution = [cow]
Is solution an array, or is it in that form? (var solution = [cow, pig]) You also need to add quotes around those values, unless those values are defined variables.
You need to change the variable to look like this:
var solution = ['cow', 'pig']
If so, just get the value at subscript 0.
var result = solution[0];
console.log(result);
If you mean an string like
solution = "cow pig".
Do
solution = solution.split(' ')[0];
console.log(solution); //Will return cow
This question already has answers here:
endsWith in JavaScript
(30 answers)
Closed 9 years ago.
Is there a javascript function to get the last occurrence of a substring in a String
Like:
var url = "/home/doc/some-user-project/project";
I would like a function that returns true if the String contains project at his end.
I know str.indexOf() or str.lastIndexOf() but is there another function that do the job or should I do it?
Thanks for the answer
Something like
var check = "project",
url = "/home/doc/some-user-project/project";
if (url.substr(-check.length) == check){
// it ends with it..
}
Try this
<script>
var url = "/home/doc/some-user-project/project";
url.match(/project$/);
</script>
The response is a array with project, if the responde is 'null' because it is not found