user3142695
user3142695

Reputation: 17332

Splitting Javascript-String with keywords out of an array

How can I split a string in JavaScript using an array-keyword list?

var keywords = [ 'An Example', 'Test'];

var str = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr\n"+
    "Test: Lorem ipsum dolor sit amet, consetetur sadipscing elitr\n"+
    "This is An Example Lorem ipsum dolor sit amet, consetetur sadipscing elitr\n"+
    "An Example Lorem ipsum dolor sit amet, consetetur sadipscing elitr";
  1. I would like to make out of every line an HTML-paragraph
  2. If there is a keyword out of the array at the beginning (!) of a line, the keyword should get its own paragraph and the ":" should be deleted (if there is one).

In my example I want to get:

<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</p>
<p>Test</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</p>
<p>This is An Example Lorem ipsum dolor sit amet, consetetur sadipscing elitr</p>
<p>An Example</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</p>

My poor solution right now is something like

str.trim().replace(/(.*?)(\n|:)/mgi, '<p>$1</p>');

Upvotes: 3

Views: 524

Answers (3)

aquinas
aquinas

Reputation: 23796

function escapeRegExp(str){
  return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

reg = keywords.map(function(s){ return escapeRegExp(s) + ":?";}).join("|");

var result = str.split(/\n/).map(function(x) { 
    var res = x.split(new RegExp("^(" + reg + ")"));
    return res.length==1 ? "<p>" + res[0] + "</p>" : "<p>" + res[1].replace(/:$/,"") + "</p>\n<p>" + res[2].trim();
}).join("\n");

http://jsfiddle.net/Tg3ch/

Upvotes: 3

McGarnagle
McGarnagle

Reputation: 102753

Here's a way, by first splitting into paragraphs, then looping through, checking for each keyword, and pushing the results back into a new array:

var result = [];
var keywords = [ 'An Example', 'Test'];
str.split('\n').forEach(function(pg) {
    var any = false;
    for (var keyword in keywords) {
        var keyword = keywords[idx];
        if (pg.indexOf(keyword) == 0) {
            result.push(keyword);
            var stripped = pg.substr(keyword.length);
            if (stripped.indexOf(": ") == 0)
                stripped = stripped.substr(2);
            result.push(stripped);
            any = true;
            break;
        }
        // no keyword matched -- push the paragraph unchanged
        if (!any) result.push(pg);
    }    
});

Fiddle

Upvotes: 2

Toothbrush
Toothbrush

Reputation: 2141

You could build the regular expression from the keyword list:

var result = ('<p>' + str + '</p>').replace(/\r?\n/g, '</p>\r\n<p>').replace(new RegExp('^<p>(' + keywords.map(function(x){return x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');}).join('|') + ')(?:\\:)?\\s*', 'mgi'), '<p>$1</p>\r\n<p>');

See http://jsfiddle.net/6uuXY/1/ for a complete example.

Upvotes: 2

Related Questions