0

I have used Regex to extract the content inside square brackets :-

string.match(/[^[\]]+(?=])/g)

but this does not extract the string between square brackets such as

string= "[Description of project (only for)]"

also what will be the regex to remove the html elements inside the square brackets

example :-

string="[<span>Description of project (only for)</span>]"

output after apllying regex should be "Description of project(only for)"

1 Answers1

1
  1. Match all content in square brackets.

  2. Use a callback function to replace html tags in this matched content.

DEMO : http://jsfiddle.net/uZRQt/

var matches = "This is <span>Outside content </span> and inside is [<span>Description of project (only for)</span>]".replace(/\[.*?\]/gi,function(match) {

    return match.replace(/<\/?[^>]*?>/gi,'');

});

alert(matches);
Community
  • 1
  • 1
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • Hey its not working for something like getcontent=
    [Assignment name] [Total No of staff-months of the assignment]
       
       
       
       
       
    in this i dont want to lose any other tag except the ones which are inside the quare brackets
    – user1744639 Oct 15 '12 at 15:18
  • @user1744639 , It does work for this input as well : http://jsfiddle.net/uZRQt/1/ – DhruvPathak Oct 16 '12 at 06:14