Sam
Sam

Reputation: 49

javascript - regular expression

Trying to form a regular expression to match the pattern of keywords, the pattern I found is like

find the term in this jsfiddle.

var newInput="keyword2:(content2) app keyword1:(content1) sos keyword:(content) das sad";

Im looking for an output like

app,sos,das,sad 

Upvotes: 0

Views: 120

Answers (2)

Tomalak
Tomalak

Reputation: 338326

newInput.replace(/[^:\s]+:\([^)]*\)\s*/g, '');  // "app sos das sad"

Explanation

[^:\s]+:   # any character (except ' ' and ':') in front of ':'  "keyword1:'"
\([^)]*\)  # any character enclosed in '(' ')'                   "(content2)"
\s*        # trailing spaces                                     " "

This returns a space-separated string. You would have to trim it and split at spaces (or replace them) yourself.

Upvotes: 2

nnnnnn
nnnnnn

Reputation: 150080

You can try:

var words = newInput.replace(/[^\s]+:\([^\)]+\)\s+/g, "").split(/\s+/);

Which will produce an array of the words as:

["app", "sos", "das", "sad"]

If you want a comma-separated string as shown in the question then:

var words = newInput.replace(/[^\s]+:\([^\)]+\)\s+/g, "").split(/\s+/).join(", ")

Upvotes: 1

Related Questions