This question already has answers here:
Split by Caps in Javascript
(4 answers)
Closed 3 years ago.
I have this string variable
var mySentence = "EverythingIsAwesome";
I want it to output: "Everything Is Awesome"
I tried doing mySentence.split(" "); but it stays the same. Then I tried: mySentence.split(" ").join(" "); but it still keeps the sentence together.
Can anyone help me figure out what I'm doing wrong? Many thanks in advance!
You should use match() like this:
mySentence.match(/[A-Z][a-z]+/g);
You will get this array: ['Everything', 'Is', 'Awesome']
To turn it back into a string simply: mySentence.join("");
Related
This question already has answers here:
Removing everything except numbers in a string
(7 answers)
Closed 3 years ago.
I want to replace the dot in a number in Javascript with regular expression; if country_temp is, for example, 1.234, how can I put the value 1234 in country_temp2?
I have tried the following:
const country_temp2 = country_temp.replace(/[a-zA-Z]*\**\s*\./g, "")
but it's not working.
Edit: the original string is composed by characters, asterisk, a number and a dot
Sorry, but I have written in a very fast way.
Try this:
country_temp.replace(/\./g,'')
This question already has answers here:
The .replace() method does change the string in place [duplicate]
(3 answers)
Closed 3 years ago.
I am having the following string 4 (10.3%) and would like to receive 4.
I tried:
let s = "4 (10.3%)"
s.replace("/\s+\((.*?)\)", "");
console.log(s)
However, I still get the initial string back.
Any suggestion what I am doing wrong?
I appreciate your replies!
Use this instead of regex
let s = "4 (10.3%)"
console.log(s.substr(0, s.indexOf(' ')))
Using split
let s = "4 (10.3%)"
console.log(s.split(" ")[0])
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
I'm gonna to write regex or other expression to get coordinates after '='.
My example:
var cords = https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac=
I want to get 50.082961,19.966860
I know that I could use slice but I think I could write it better with regex.
Simple base for this example: \=(.[0-9]) What's next?
Try this center\=(\d+\.\d+,\d+\.\d+)&
var val = 'https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac='.match(/center\=(\d+\.\d+,\d+\.\d+)&/)[1]
console.log(val)
But as other's have commented, you likely shouldn't be using regex for this purpose
This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 6 years ago.
Having a string like:
"*this* not that"
I can select *this*
\*(.*?)\*
but I'm not able to get only this.
what I am trying to achieve is to replace -this- by a new string. What's the easiest way to do that ?
you can try:
"*this* not that".replace(/\*.*\*/,'*new_string*');
//"*new_string* not that"
This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method