-4

I need to extract from string

userAllowedCrud['create'] the part that is inside [].

i think using regular expression is the better way to do it. Am i wrong?

Leonel Matias Domingos
  • 1,502
  • 5
  • 21
  • 47
  • _“i think using regular expression is the better way to do it”_ - for one thing to be better than another, you need at least two things ... so, what would the second/the others? – CBroe Feb 14 '18 at 11:20

3 Answers3

1

For the example string, you could use split which will return an array and specify the single quote ' as the separator.

Your value will be the second item in the array.

var string = "userAllowedCrud['create']";
console.log(string.split("'")[1]);

If you want to use a regex you could use: ^[^\[]+\['([^']+)']$ or \['([^']+)']

Your value will be in group 1

The first regex will match:

^       # Begin of the string
[^[]+   # Match not [ one or more times
['      # Match ['
(       # Capture in a group (group 1)
  [^']+ # Match not a ' one or more times
)       # Close capturing group
']      # Match ']
$       # End of the string
 

The second regex captures in a group what is between [''] without ^ and $

var string = "userAllowedCrud['create']";
var pattern1 = /^[^\[]+\['([^']+)']$/;
var pattern2 = /\['([^']+)']/
console.log(string.match(pattern1)[1]);
console.log(string.match(pattern2)[1]);
The fourth bird
  • 96,715
  • 14
  • 35
  • 52
0

You could use a regular expression like: /\[([^\]]*)\]/. \[ means match a [, \] means match a ], [^\]]* means match 0 or more of any character that is not a close bracket.

console.log(
    "userAllowedCrud['create']".match(/\[([^\]]*)\]/)[1]
);

// Output:
// 'create'

If you need what is inside the quotes inside the brackets there are many solutuions, for example:

// for single and double quotes
"userAllowedCrud['create']".match(/\[([^\]]*)\]/)[1].slice(1, -1)

// Or (for single and double quotes):
"userAllowedCrud['create']".match(/\[("|')([^\]]*)\1\]/)[2]

// Or (for single and double quotes):
"userAllowedCrud['create']".match(/\[(["'])([^\]]*)\1\]/)[2]

// Or (for single quotes):
"userAllowedCrud['create']".match(/\['([^\]]*)'\]/)[1]

// Or (for double quotes):
"userAllowedCrud['create']".match(/\['([^\]]*)'\]/)[1]

There are many other methods, these are just a few. I'd recommend looking into learning regex: https://stackoverflow.com/a/2759417/3533202

scagood
  • 724
  • 4
  • 11
  • Thanks @scagood . But i need it without ' just the create not 'create' – Leonel Matias Domingos Feb 14 '18 at 11:34
  • @LeonelMatiasDomingos - You could use something like: `.match(/\[(["'])([^\]]*)\1\]/)[2]`. instead. I would strongly recommend looking at https://stackoverflow.com/a/2759417/3533202 for more information on regex – scagood Feb 14 '18 at 11:39
0

Try to use javascript string operation

let tempString = "userAllowedCrud['create']";
let key = str => str.substring(
    str.indexOf("'") + 1,
    str.lastIndexOf("'")
);
console.log(key(tempString))
scagood
  • 724
  • 4
  • 11