pradip
pradip

Reputation: 197

regular expression to find a repeated code

I am trying to write a reg expression to find match of strings / code in a database.

here is some of the sample code / string which i need to remove using the regular expression.

[b:1wkvatkt] 
[/b:1wkvatkt]
[b:3qo0q63v]
[/b:3qo0q63v]
[b:2r2hso9d]
[/b:2r2hso9d]

Anything that match [b:********] and [/b:********]

Anybody please help me out. Thanks in advance.

Upvotes: 0

Views: 39

Answers (1)

ssc-hrep3
ssc-hrep3

Reputation: 16089

You can use the following pattern (as stated by LukStorms in the comments):

\[\/?b:[a-z0-9]+\]

If you want to replace [b:********] with <b> (and also the closing one), you can use the following snippet (here in JavaScript, other languages are similar):

var regex = /\[(\/)?b:[a-z0-9]+\]/g;
var testText = "There was once a guy called [b:12a345]Peter[/b:12a345]. He was very old.";

var result = testText.replace(regex, "<$1b>");
console.log(result);

It matches an optional / and puts it into the first group ($1). This group can then be used in the replacement string. If the slash is not found, it won't be added, but if it is found, it will be added to <b>.

Upvotes: 1

Related Questions