Regex after first sign [duplicate] - javascript

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

Related

how to convert "12A,13B,14C" to [12A,13B,14C] in javascript [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 5 years ago.
I have a string like below.
sections = "12A,13B,14C"
But I need [12A, 13B, 14C] for future use. How to create array from above string?
You have to use split method for that to split your string data into array values, have a look to updated code
var sections = "12A,13B,14C";
sections = sections.split(',');
console.log(sections);
Hope this will help! Just use split method of JS.
sections = "12A,13B,14C"
let array = sections.split(',');
console.log(array)

Counting sub-strings [duplicate]

This question already has answers here:
How to count string occurrence in string?
(40 answers)
Closed 6 years ago.
If you want to count the number of occurrences of one string inside another, which approach is better in terms of simplicity and/or performance? -
using indexOf in a for/while loop
using a regular expression
And if it is the latter, then what is the best way to do it?
Try the following regular expression:
var temp = "This is a string.";
var count = (temp.match(/is/g) || []).length;
alert(count);

extracting text between two characters [duplicate]

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"

Number format with regex javascript [duplicate]

This question already has answers here:
Javascript Thousand Separator / string format [duplicate]
(15 answers)
Closed 8 years ago.
I have a number is 1205000000, I want display at 1.205.000.000
number.toString().replace(/(\d{3})/g, "$1.").toString()
but result is 120.500.000.0
I don't want reverse a number.
For the sake of correcting your regular expression (obviously for integer values only):
number.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1.');
ยป Detailed Regex Explanation
One way would be to reverse the string before your manipulation and ther reverse it again. Like so:
var number = 1205000000;
function reverse(s) {
return s.split("").reverse().join("");
}
var str = reverse(reverse(number.toString()).replace(/(\d{3})/g, "$1."));
alert(str);
See this working fiddle.
EDIT:
See the comments. Its a bit dirty but for that specific number it will work. The link posted by #Artyom Neustroev as a comment under you question seems a whole lot better than this here.

JavaScript / jQuery: how to split string with multiple values [duplicate]

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

Categories

Resources