-2

I would like to find some words in my string. Example: {add_First} and/or {ban_1}

Regular expression for first: var arr = str.match(/{add_([A-Z]+)}/gi);

Regular expression for second: var arr = str.match(/{ban_([0-9]+)}/gi);

But how to combime both regular expressions?

halfer
  • 19,824
  • 17
  • 99
  • 186
XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60

1 Answers1

1
/{(add_([A-Z]+)|ban_([0-9]+))}/gi

Here is a Regexper visualisation:

enter image description here

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • And what will you get out of it? – VisioN Jun 02 '14 at 11:15
  • 1
    I assume the OP wants to get a whatever function that will return an array of something like `[1, 'First', 2, 'Second', ...]` or `[[1, 'First'], [2, 'Second'], ...]`. Your regular expression will return the array of full matched groups, i.e. `['{add_First}', '{ban_1}', ...]`. – VisioN Jun 02 '14 at 11:19
  • @VisioN I'm still not sure what you mean. It appears to me that OP just wants to check whether text exists within a string. If the `str` variable is set to `Hello, {add_First} world!`, for instance, passing in the regular expression from my answer into `str.match(...)` will return an array containing `"{add_First}"`. – James Donnelly Jun 02 '14 at 11:27
  • But VisioN is right I can't get an ID using your code? How can I change this? – XTRUST.ORG Jun 02 '14 at 11:47
  • str = str.replace(/{(add_([A-Z]+)|ban_([0-9]+))}/gi, function(match, id) { //console.log(id); var o = Core.get_data_by_id(ex.extensions, id); return (o && o.status == "1") ? o.html : match; }); – XTRUST.ORG Jun 02 '14 at 11:49
  • @user889349 I'm not really sure what you're trying to achieve, but it sounds like you need to a lookahead/lookbehind (see http://stackoverflow.com/questions/2973436/regex-lookahead-lookbehind-and-atomic-groups). – James Donnelly Jun 02 '14 at 11:50
  • I would like to get an array like this: [1, 'First', 2, 'Second', ...]. Now I have: ['{add_First}', '{ban_1}', ...] – XTRUST.ORG Jun 02 '14 at 11:53