RegExp#exec
and String#match
JS-D007 75 },
76 });
77 md.use(container, "info", {
78 validate: (params) => params.trim().match(/^info\s+(.*)$/), 79 render: (tokens, idx) => {
80 const m = tokens[idx].info.trim().match(/^info\s+(.*)$/);
81 if (tokens[idx].nesting === 1) {
64 },
65 });
66 md.use(container, "danger", {
67 validate: (params) => params.trim().match(/^danger\s+(.*)$/), 68 render: (tokens, idx) => {
69 const m = tokens[idx].info.trim().match(/^danger\s+(.*)$/);
70 if (tokens[idx].nesting === 1) {
53 },
54 });
55 md.use(container, "warning", {
56 validate: (params) => params.trim().match(/^warning\s+(.*)$/), 57 render: (tokens, idx) => {
58 const m = tokens[idx].info.trim().match(/^warning\s+(.*)$/);
59 if (tokens[idx].nesting === 1) {
42 }
43 });
44 md.use(container, "tip", {
45 validate: (params) => params.trim().match(/^tip\s+(.*)$/), 46 render: (tokens, idx) => {
47 const m = tokens[idx].info.trim().match(/^tip\s+(.*)$/);
48 if (tokens[idx].nesting === 1) {
86 },
87 });
88 md.use(container, "details", {
89 validate: (params) => params.trim().match(/^details\s+(.*)$/), 90 render: (tokens, idx) => {
91 const m = tokens[idx].info.trim().match(/^details\s+(.*)$/);
92 if (tokens[idx].nesting === 1) {
RegExp#exec
and String#match
should only be used when we need to use the parts of a string that match a specific pattern:
const matches = /[a-zA-Z0-9]+/.exec(string)
for (const match of matches) {
processMatch(match)
}
If you only want to know whether a string matches a particular pattern, RegExp#test
is a faster alternative.
const matches = str.match(/[a-zA-Z0-9]/) ? process(str) : process("default-str");
const strMatchesPattern = !!str.match(/regex/)
const regexp = new RegExp("[a-zA-Z0-9]*")
if (regexp.exec(myString)) {
// ...
}
const matches = '/hasTheMagic/'.test(str) ? process(str) : process("default-str");
const strMatchesPattern = /regex/.test(str)
const regexp = new RegExp("[a-zA-Z0-9]*")
if (regexp.test(myString)) {
// ...
}