This question already has answers here:
Regular Expression to get a string between parentheses in Javascript
(10 answers)
Closed 4 years ago.
How can I modify this string:
"SRID=4326;POINT (-21.93038619999993 64.1444948)"
so it will return
"-21.93038619999993 64.1444948"
(and then I can split that)?
The numbers in the string can be different.
I've tried using .replace & split, but I couldn't get it to work properly. How can I make this happen using Javascript?
You can try with match and regex:
"SRID=4326;POINT (-21.93038619999993 64.1444948)".match(/\(([^)]+)\)/)[1]
// "-21.93038619999993 64.1444948"
I am not good using REGEXP but this could be a solution with pure split.
Hope it helps :>
var str = "SRID=4326;POINT (-21.93038619999993 64.1444948)" ;
var newStr = str.split('(')[1].split(')')[0];
console.log(newStr)
var new_string = string.replace("SRID=4326;POINT (", "");
You can use a regular expression. The first number is put in first, the second number is put in second.
const INPUT = "SRID=4326;POINT (-21.93038619999993 64.1444948)";
const REGEX = /SRID=\d+;POINT \((.+) (.+)\)/
const [_, first, second] = INPUT.match(REGEX);
console.log(first);
console.log(second);
Related
This question already has answers here:
Replace multiple characters in one replace call
(21 answers)
Closed 1 year ago.
I'm a beginner with Javascript and I have following Question:
I have to replace the patterns #XY1, #XY2, #XY3 or #XY4 with the SAME replacement in a string.
Eg. replacement with AA:
this is #XY1 an #XY3 example #XY1 --> this is AA an AA example AA
The patterns occur randomly and it is possible to have several (different) once in the string. How do I replace them without iterating too often through the string? So i DO NOT look for suggestions like:
str = str.replace(/:#XY1:/g, "Replacement")
str = str.replace(/:#XY2:/g, "Replacement")
str = str.replace(/:#XY3:/g, "Replacement")
str = str.replace(/:#XY4:/g, "Replacement")
or something with a loop.
I hope to find a solution where I just have to iterate ONCE through the string and replace all patterns #XY PLUS the next variable character.
I can not use .replaceAll because of some version problems.
Thank you for your help and have a nice day! Cheers, Josh
Just use one regex and wildcard the digit.
const repl = (source, value) => source.replace(/#XY\d/g, value);
console.log(repl('this is #XY1 an #XY3 example #XY1', 'AA')) // this is AA an AA example AA
You can use
/#XY\d/
const str = "this is #XY1 an #XY3 example #XY1";
const replacement = "AA";
const result = str.replace(/#XY\d/g, replacement);
console.log(result);
This question already has answers here:
How to tell if a string contains a certain character in JavaScript?
(21 answers)
Closed 4 years ago.
I'm a beginner in node.js so please do excuse me if my question is foolish. As we know we can use
var regex = /[ !##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/g;
regex.test(str);
to check whether a string contains special charecters or not .But what I'm asking is how to check for only a particular charecter means how can I check only presence of #.
I tried to do
var regex = /[#]/g; regex.test(str).
Although it's not working but are there any other method of doing this?
You don't need a regex to find a single character in a string. You can use indexOf, like this:
var hasHashtag = str.indexOf('#') >= 0;
This returns true if the character is in the string.
Use includes to check the existence of # in your string. You don't actually require regex to do that.
var str = 'someSt#ring';
var res = str.includes('#');
console.log(res);
str = 'someSt#ri#ng';
res = str.includes('#');
console.log(res);
str = 'someString';
res = str.includes('#');
console.log(res);
Use indexOf
str.indexOf('#') >= 0;
This question already has answers here:
Regular expression greater than and less than
(3 answers)
Closed 8 years ago.
I need a regular expression that replaces the greater than and less than symbol on a string
i already tried
var regEx = "s/</</g;s/>/>/g"
var testString = "<test>"
alert(testString.replace(regEx,"*"))
My first time to use it please go easy on me :)
Thanks
You can use regEx | like
var regEx = /<|>/g;
var testString = "<test>"
alert(testString.replace(regEx,"*"))
Fiddle
For greater than and less than symbol.
var string = '<><>';
string = string.replace(/[\<\>]/g,'*');
alert(string);
For special characters
var string = '<><>';
string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_');
alert(string);
Insert the regular expression in the code before class
using System.Text.RegularExpressions;
below is the code for string replace using regex
string input = "Dot > Not Perls";
// Use Regex.Replace to replace the pattern in the input.
string output = Regex.Replace(input, "some string", ">");
source :
http://www.dotnetperls.com/regex-replace
This question already has answers here:
Find and get only number in string
(4 answers)
Closed 8 years ago.
I have the following string
/Date(1317772800000)/
I want to use a Javascript regular expression to extract the numerical portion of it
1317772800000
How is this possible?
That should be it
var numPart = "/Date(1317772800000)/".match(/(\d+)/)[1];
No need for regex. Use .substring() function. In this case try:
var whatever = "/Date(1317772800000)/";
whatever = whatever.substring(6,whatever.length-2);
This'll do it for you: http://regex101.com/r/zR0wH4
var re = /\/Date\((\d{13})\)\//;
re.exec('/Date(1317772800000)/');
=> ["/Date(1317772800000)/", "1317772800000"]
If you don't care about matching the date portion of the string and just want extract the digits from any string, you can use this instead:
var re = /(\d+)/;
re.exec('/Date(1317772800000)/')
["1317772800000", "1317772800000"]
This question already has answers here:
How can I get last characters of a string
(25 answers)
Closed 10 years ago.
If I have the following variable in javascript
var myString = "Test3";
what is the fastest way to parse out the "3" from this string that works in all browsers (back to IE6)
Since in Javascript a string is a char array, you can access the last character by the length of the string.
var lastChar = myString[myString.length -1];
It does it:
myString.substr(-1);
This returns a substring of myString starting at one character from the end: the last character.
This also works:
myString.charAt(myString.length-1);
And this too:
myString.slice(-1);
var myString = "Test3";
alert(myString[myString.length-1])
here is a simple fiddle
http://jsfiddle.net/MZEqD/
Javascript strings have a length property that will tell you the length of the string.
Then all you have to do is use the substr() function to get the last character:
var myString = "Test3";
var lastChar = myString.substr(myString.length - 1);
edit: yes, or use the array notation as the other posts before me have done.
Lots of String functions explained here
myString.substring(str.length,str.length-1)
You should be able to do something like the above - which will get the last character
Use the charAt method. This function accepts one argument: The index of the character.
var lastCHar = myString.charAt(myString.length-1);
You should look at charAt function and take length of the string.
var b = 'I am a JavaScript hacker.';
console.log(b.charAt(b.length-1));