How to extract substring between specific characters in javascript - javascript

How to extract "51.50431" and "-0.1133" from LatLng(51.50431, -0.1133)
using jquery.
Tried using substring() but not helpful as numbers in LatLng(51.50431, -0.1133) keep on changes in different ranges. Like some time it can come as LatLng(51.50, -0.1).
Any help?

Regular expressions to the rescue:
'LatLng(51.50, -0.1)'.match(/LatLng\(([^,]+),\s*([^)]+)\)/)
// ["LatLng(51.50, -0.1)", "51.50", "-0.1"]

Related

Get an example matched text from a regex pattern [duplicate]

Is there any way of generating random text which satisfies provided regular expression.
I am looking for a function which works like below
var reg = Some Regular Expression
var str = RandString(reg)
I have seen fairly good solutions in perl and ruby on github, but I think there are technical issues that make a complete solution impossible. For example, /[0-9]+/ has an infinite upper bound, which is not practical for selecting random numbers from.
Never seen it in JavaScript, but you could translate.
EDIT: After googling for a few seconds...
https://github.com/fent/randexp.js
if you know what the regular expression is, you can just generate random strings, then use a function that references the index of the letters and changes them as needed. Regex expressions vary widely, so it will be difficult to find one in particular that satisfies all possible regex.
Your question is pretty open so hopefully this steers you to the right solution. Get the current time (in seconds), MD5 it, check it against a REGEX, return the match.
Running Example: http://jsfiddle.net/MattLo/3gKrb/
Usage: RandString(/([A-Za-z])/ig); // expected to be a string
For JavaScript, the following modules can generate a random match to a regex:
pxeger
randexp.js
regexgen

Match a word unless it is preceded by an equals sign?

I have the following string
class=use><em>use</em>
that when searched using us I want to transform into
class=use><em><b>us</b>e</em>
I've tried looking at relating answers but I can't quite get it working the way I want it to. I'm especially interested in this answer's callback approach.
Help appreciated
This is a good exercise for writing regular expressions, and here's a possible solution.
"useclass=use><em>use</em>".replace(/([^=]|^)(us)/g, "$1<b>$2</b>");
// returns "<b>us</b>eclass=use><em><b>us</b>e</em>"
([^=]|^) ensures that the prefix of any matched us is either not an equal sign, or it's the start of the string.
As #jamiec pointed out in the comments, if you are using this to parse/modify HTML, just stop right now. It's mathematically impossible to parse a CFG with a regular grammar (even with enhanced JS regexps you will have a bad time trying to achieve that.)
If you can make any assumptions about the structure of your document, you may be better off using an approach that operates on DOM elements directly rather than parsing the whole document with a regex.
Parsing HTML with a regex has certain problems that can be painful to deal with.
var element = document.querySelector('em');
element.innerHTML = element.innerHTML.replace('us', '<b>us</b>');
<div class=use><em>use</em>
</div>
I would first look for any character other than the equals sign [^=] and separate it by parentheses so that I can use it again in my replacement. Then another set of parentheses around the two characters us ought to do it:
var re = /([^=]|^)(us)/
That will give you two capture groups to work with (inside the parentheses), which you can represent with $1 and $2 in your replacement string.
str.replace( /([^=|^])(us)/, '$1<b>$2</b>' );

javascript number regular expression

I've come up with this regular expression to validate a javascript number according to the specification:
(-|\+|)(\d+\.?\d*|\.\d+)([eE](-|\+|)\d+)?
As far as I can think of, these are valid numbers in js:
123,123.3, .3, -123, -.3, -.3e-2, -.3e+2, +.2e2... and so forth.
I've been trying to find a verified regular expression on the internet so that I could compare my solution but to no avail.
Could anyone tell me if my approach is correct or give me a better solution?
Link to test my solution
While using isNan is the correct way of checking numbers in JavaScript, you can also validate floating point numbers with [-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)? regex (taken from Regular-Expressions.info).
Consider using appropriate anchors though! (^ for string start, $ for string end).
Demo is available here.

Split string with various delimiters while keeping delimiters

I have the following string:
"dogs#cats^horses^fish!birds"
How can I get the following array back?
['dogs','#cats','^horses','^fish','!birds']
Essentially I am trying to split the string while keeping the delimeters. I've tried string.match with no avail.
Assuming those are your only separators then you can do this:
var string = "dogs#cats^horses^fish!birds";
string.replace(/(#|\^|!)/g, '|$1').split('|');
We basically add our own separator, in this case | and split it based on that.
This does what you want:
str.match(/((^|[^\w])\w+)/g)
Without more test cases though, it's hard to say how reliable it would be.
This is also assuming a large set of possible delimiters. If it's a small fixed amount, Samer's solution would be a good way to go

Replace a string's special characters using regex

the string looks something like below:
/1/2/3/4 however I want to replace this with ?1=2&3=4.
I am planning to use REReplace in ColdFusion.
Could you suggest me a regex for this ?I also thought of using loops but stuck either way...
Thanks in Advance
A bit cumbersome without making it more manageable using a loop as #Leigh suggested; but you can use the following on string inputs that contain even occurrences of n/m in the format you described:
var s = "/1/2/3/4/5/6";
s.replace(/^\//,'?').replace(/(\d+)\/(\d+)/g,'$1=$2').replace(/\//g,'&')
// => "?1=2&3=4&5=6"

Categories

Resources