Philip
Philip

Reputation: 7166

Regex - Exclude brackets and brackets with special key

I got this string:

[[Fil:Hoganas_hamn.jpg|miniatyr|Höganäs Hamn.]] [[Fil:Hoganas_hamn_kvickbadet.jpg|miniatyr|Höganäs Hamn - Kvickbadet.]] [[Fil:Höganäs Jefast ny redigerad-1.jpg|miniatyr|Jefasthuset sett från väster med en del av den nya bryggan vid Kvickbadet.]] '''Höganäs''' är en [[tätort]] och [[centralort]] i [[Höganäs kommun]] i [[Skåne län]]. Höganäs blev stad 1936. Ursprungligen är Höganäs ett [[fiskeläge]] kring vilket en [[gruvindustri]] utvecklades för brytning av [[kol (bränsle)|kol]] och [[lera|leror]] för tillverkning av [[eldfast]] [[keramik]] ([[Höganäskrus]]). Gruvindustrin är numera nedlagd.

I want to exclude every instance of [[FIL: + dynamic word]] and every [[, ]], but not exclude the word itself when its only [[word]] without the "FIL:" in it.

I've begun doing a regex for it but I'm stuck.

\[\[\Fil:|\]\]

The output Im after should look like this:

'''Höganäs''' är en tätort och centralort i Höganäs kommun i Skåne län. Höganäs blev stad 1936. Ursprungligen är Höganäs ett fiskeläge kring vilket en gruvindustri utvecklades för brytning av kol (bränsle)|kol och lera|leror för tillverkning av eldfast keramik (Höganäskrus). Gruvindustrin är numera nedlagd.

I have JQuery but think .replace should do the trick?

Upvotes: 1

Views: 405

Answers (3)

David M
David M

Reputation: 72840

Try replacing all matches for this Regex with an empty string:

\[\[Fil:[^\]]*\]\]|\[\[|\]\]

To break this down:

\[\[Fil:[^\]]*\]\]   matches [[Fil:...]]
\[\[                  matches remaining [[
\]\]                  matches remaining ]]
|                     combines with OR

To get your exact output, you may need to strip some whitespace as well:

\[\[Fil:[^\]]*\]\]\s+|\[\[|\]\]

So, in JavaScript, you could write:

x.replace(/\[\[Fil:[^\]]*\]\]\s+|\[\[|\]\]/g, '');

Upvotes: 1

abidibo
abidibo

Reputation: 4287

Try this, maybe you want also to adjust spaces

var string = "[[Fil:Hoganas_hamn.jpg|miniatyr|Höganäs Hamn.]] [[Fil:Hoganas_hamn_kvickbadet.jpg|miniatyr|Höganäs Hamn - Kvickbadet.]] [[Fil:Höganäs Jefast ny redigerad-1.jpg|miniatyr|Jefasthuset sett från väster med en del av den nya bryggan vid Kvickbadet.]] '''Höganäs''' är en [[tätort]] och [[centralort]] i [[Höganäs kommun]] i [[Skåne län]]. Höganäs blev stad 1936. Ursprungligen är Höganäs ett [[fiskeläge]] kring vilket en [[gruvindustri]] utvecklades för brytning av [[kol (bränsle)|kol]] och [[lera|leror]] för tillverkning av [[eldfast]] [[keramik]] ([[Höganäskrus]]). Gruvindustrin är numera nedlagd.";

var result = string.replace(/\[\[Fil:.*?\]\]/g, '').replace(/\[\[(.*?)\]\]/g, '$1');

console.log(result);

Upvotes: 1

mpen
mpen

Reputation: 282825

You can use a regex like this

\[\[.*?\]\]

And then use the callback function version of replace to check if starts with Fil: then conditionally decide whether you want to return a blank string to erase it, or just the word itself.

Alternately, use 2 regexes. Replace the Fil: ones with a blank string first, and then the rest with just the word. You can use

\[\[(\w+)\]\]

Or something similar to catch the [[word]] ones and then replace it with a backreference to the word, i.e., \1 refers to what's in parentheses.

Upvotes: 0

Related Questions