4
If I have a phrase: "melhor 01/01/2016 - 05/01/2016"
How can I retrieve the dates of this phrase using regex?
thus: var data1="01/01/2016" , data2="05/01/2016"
4
If I have a phrase: "melhor 01/01/2016 - 05/01/2016"
How can I retrieve the dates of this phrase using regex?
thus: var data1="01/01/2016" , data2="05/01/2016"
5
You can do this using the method String.match
. You need to pass a regular expression which, when evaluated, will return a array
disputes the values that matched the expression passed.
Example:
var regex_date = /(0[1-9]|[1-2]\d{1}|3[01])\/(0[1-9]|1[1-2])\/\d{4}/g
var dates = 'Vamos sair em 04/02/2015 e voltar em 04/05/2015'.match(regex_date)
for (date in dates) {
document.write(dates[date] + "<br>")
}
Can also be done with the method exec
regex: regex_date.exec(texto)
. The result is the same.
Interesting @Pedrocamarajunior, add your answer. Because it’s even better, because instead of calling the string, it can simply reuse the regex
I think it looks better as a complement to yours. Reference: Regexp.prototype.exec()
I get it. Thank you!
I tested here @Pedrocamarajunior, it didn’t work the same way as my example. I was going to put the reference, but it would give wrong information
I think it’s yours Regex
, he’s grouping wrong (I don’t know exactly where, I’m not very good with regex). I used this /([0-3]{1}[0-9]{1}\/[0-1]{1}[0-9]{1}\/[0-9]{4})/
to test and in both cases brings the same result.
@Pedrocamarajunior ah, but then you changed the regex. In my case, the first one worked, so much so that has in the example
Yes. Then put a new answer then. :)
Browser other questions tagged javascript regex
You are not signed in. Login or sign up in order to post.
Possible duplicate of How to select a text snippet in a String?
– Pedro Camara Junior