3

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>');
2
  • May I ask you why you chose the answer @aquinas gave rather than mine? I answered first with almost the same code as his. Commented Feb 17, 2014 at 10:05
  • I'm very sorry. I made a mistake. Everything is fine with your solution. Thank you! Commented Feb 17, 2014 at 10:13

3 Answers 3

3
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/

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.