This question already has answers here:
How do I split a string with multiple separators in JavaScript?
(25 answers)
Closed 3 years ago.
How to split strings of format like "12-12-1990" or "12/12/1990" where - and / are the delimiters.
I have seen answers using regex but I am not proficient in it. Answers regarding this will be helpful as I am a beginner
try this
"2020-01-31".split(/[/-]/ig)
var dateParts1 = "2020-01-31".split(/[/-]/ig);
console.log(dateParts1);
var dateParts2 = "2020/02/21".split(/[/-]/ig);
console.log(dateParts2);
Regex explain
You can pass a regex into Javascript's split operator. For example:
"12-12-1990".split(/-|\//)
["12", "12", "1990"]
"12/12/1990".split(/-|\//)
["12", "12", "1990"]
You can use split with the delimiter on which you want to split the string
var x="12-12-1990";
console.log(x.split('-'))
Related
This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 3 years ago.
My string should be in the IRC command format : "/add john".
So, i created this Regex :
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test('\/add user1');
alert(bool);
The problem is either I use /***/ or RegExp syntax, if I set a backslash at the beginning of my string (like in my example above), my alert pop up show "true" and I don't want that.
I code in Javascript
You can use String.raw to make sure that the backlash is not removed when testing your input:
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test(String.raw`\/add user1`);
alert(bool);
You can play with this code here: https://jsbin.com/ziqecux/25/edit?js
This question already has answers here:
Regular Expression to find a string included between two characters while EXCLUDING the delimiters
(13 answers)
Closed 5 years ago.
var faxNum = $("#txt_faxnum").val();
//Value : ""kekurt kekt809" <(732) 588-8300>"
I want to split between "< >". Example: Just I need to this section : (732) 588-8300
How to split this string ?
You can try this
var String=faxNum.substring(str.lastIndexOf("<")+1,faxNum.lastIndexOf(">"));
This question already has answers here:
Javascript - return string between square brackets
(6 answers)
Closed 4 years ago.
i want to find all occurrences of [anything-here] in sentence
I [am] John. [yesterday morning] я [русский] and Georgian [ქართული] and Indian [utf8-chars] and with symbols or space [good morning] or [hello, how are you?]
the result must be
[am]
[yesterday morning]
[русский]
[ქართული]
etc.
Thanks
var match = myString.match(/\[.+?\]/g);
Technically, this code will match all sentences between [ and ]
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:
Closed 10 years ago.
Possible Duplicate:
How can I match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all()?
I am trying to parse an xml document like this:
var str = data.match("<string>" + "(.*?)" + "</string>");
console.log(str);
I want to get all the elements between the [string] in an array but for some reason, it only returns the first string element found. Im not good with regular expressions so Im thinking this is just a small regex issue.
You want it to be global g
var str="<string>1</string><string>2</string><string>3</string>";
var n=str.match(/<string>(.*?)<\/string>/g);
//1,2,3
You have to form the RegEx adding a g to it like
/Regex/g