0

I am still a beginner :)

I need to get a substring ignoring the last section inside [] (including the brackets []), i.e. ignore the [something inside] section in the end.

Note - There could be other single occurances of [ in the string. And they should appear in the result.

Example

Input of the form -

1 checked arranged [1678]

Desired output -

1 checked arranged

I tried with this

var item = "1 checked arranged [1678]";

var parsed = item.match(/([a-zA-Z0-9\s]+)([(\[d+\])]+)$/);
                          |<-section 1  ->|<-section 2->|

alert(parsed);

I tried to mean the following -

section 1 - multiple occurrences of words (containing literals and nos.) followed by spaces

section 2 - ignore the pattern [something] in the end.

But I am getting 1678],1678,] and I am not sure which way it is going.

Thanks

Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144

7 Answers7

2

OK here is the problem in your expression

([a-zA-Z0-9\s]+)([(\[d+\])]+)$

The Problem is only in the last part

([(\[d+\])]+)$
 ^        ^
 here are you creating a character class, 
 what you don't want because everything inside will be matched literally.

((\[d+\])+)$
 ^      ^^
here you create a capturing group and repeat this at least once ==> not needed

(\[d+\])$
   ^
  here you want to match digits but forgot to escape

That brings us to

([a-zA-Z0-9\s]+)(\[\d+\])$

See it here on Regexr, the complete string is matched, the section 1 in capturing group 1 and section 2 in group 2.

When you now replace the whole thing with the content of group 1 you are done.

stema
  • 90,351
  • 20
  • 107
  • 135
1

You could do this

var s = "1 checked arranged [1678]";

var a = s.indexOf('[');

var b = s.substring(0,a);

alert(b);

http://jsfiddle.net/jasongennaro/ZQe6Y/1/

This s.indexOf('['); checks for where the first [ appears in the string.

This s.substring(0,a); chops the string, from the beginning to the first [.

Of course, this assumes the string is always in a similar format

Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
1
var item = '1 check arranged [1678]',
    matches = item.match(/(.*)(?=\[\d+\])/));

alert(matches[1]);

The regular expression I used makes use of a positive lookahead to exclude the undesired portion of the string. The bracketed number must be a part of the string for the match to succeed, but it will not be returned in the results.

James Sumners
  • 14,485
  • 10
  • 59
  • 77
0

Here you can find how to delete stuff inside square brackets. This will leave you with the rest. :) Regex: delete contents of square brackets

Community
  • 1
  • 1
lamelas
  • 872
  • 6
  • 15
0

try this if you only want to get rid of that [] in the end

var parsed = item.replace(/\s*\[[^\]]*\]$/,"")
petho
  • 677
  • 4
  • 10
0
var item = "1 checked arranged [1678]";
var parsed = item.replace(/\s\[.*/,"");
alert(parsed);

That work as desired?

Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
0

Use escaped brackets and non-capturing parentheses:

var item = "1 checked arranged [1678]";
var parsed = item.match(/([\w\s]+)(?:\s+\[\d+\])$/);
alert(parsed[1]); //"1 checked arranged"

Explanation of regex:

([\w\s]+)    //Match alphanumeric characters and spaces
(?:          //Start of non-capturing parentheses
\s*          //Match leading whitespace if present, and remove it
\[           //Bracket literal
\d+          //One or more digits
\]           //Bracket literal
)            //End of non-capturing parentheses
$            //End of string
Digital Plane
  • 37,354
  • 7
  • 57
  • 59