Using JavaScript RegEx to split & clean up HTML5 DB column name-attribute pairs
I'm RegEx illiterate. Matter of fact, RegEx gives me hives.
I need to split the following string at the 1st space and trim both sides of the output.
"id INTEGER PRIMARY KEY AUTOINCREMENT" <-- 2 spaces between "id开发者_StackOverflow社区" and "INTEGER"
should become
["id", "INTEGER PRIMARY KEY AUTOINCREMENT"]
Can any of the RegEx gurus help?
Update after Kai's response There can be 1 to n number of spaces between the column name and the attribute.
(\w+)\s+(.+)
To test http://rubular.com/
Is it always two spaces only at the beginning? In that case, you can split using two spaces.
"id INTEGER PRIMARY KEY AUTOINCREMENT".split(" ");
Edited response after updated question:
var str = "id INTEGER PRIMARY KEY AUTOINCREMENT",
// Regex, very specific to your string, case-sensitive
regex1 = /(id)\s+(INTEGER\sPRIMARY\sKEY\sAUTOINCREMENT)/,
// Regex, not specific to your string, but follows pattern
// and case-insensitive (/i). Same as zawhtut's response
regex2 = /(\w+)\s+(.+)/i,
results1 = str.match(regex1),
results2 = str.match(regex2);
// Test (0th position is str, results start at 1)
alert(results1[1]); // id
alert(results1[2]); // INTEGER PRIMARY KEY AUTOINCREMENT
alert(results2[1]); // id
alert(results2[2]); // INTEGER PRIMARY KEY AUTOINCREMENT
精彩评论