views:

59

answers:

4

Hello,

Apart from what they are, I dont know anything about regular expressions... :(

I have this code in a javascript function:

var foroFormatting = function (text) {
    var newText = text;
    var findreps = [
        { find: /^([^\-]+) \- /g, rep: '<span class="ui-selectmenu-item-header">$1</span>' },
        { find: /([^\|><]+) \| /g, rep: '<span class="ui-selectmenu-item-content">$1</span>' },
        { find: /([^\|><\(\)]+) (\()/g, rep: '<span class="ui-selectmenu-item-content">$1</span>$2' },
        { find: /([^\|><\(\)]+)$/g, rep: '<span class="ui-selectmenu-item-content">$1</span>' },
        { find: /(\([^\|><]+\))$/g, rep: '<span class="ui-selectmenu-item-footer">$1</span>' }
    ];
    for (var i in findreps) {
        newText = newText.replace(findreps[i].find, findreps[i].rep);
    }
    return newText;
}

This code expect a string like this

John Doe - 78 West Main St Apt 3A | Bloomsburg, PA 12345 (footer text)

and split it in four span elements right?

I would like to apply the same formatting to a string that is a bit different from that

John Doe - 78 West Main St Apt 3A | Bloomsburg, PA 12345

How do I have to modify the regular expression?

EDIT

I am trying to use this plugin (third example) with a string of mine that is different from the original just in the last part

A: 

Have you considered modifying the input format, maybe JSON? That looks like a very flimsy parsing format.

What exactly is the expected output? Use the required spans

Drew
please have a look to my edit at the end of the question
Lorenzo
+1  A: 

If all you are dropping is just (footer text), then the same function without the last replacement regex will work.

var foroFormatting2 = function (text) {
    var newText = text;
    var findreps = [
        { find: /^([^\-]+) \- /g, rep: '<span class="ui-selectmenu-item-header">$1</span>' },
        { find: /([^\|><]+) \| /g, rep: '<span class="ui-selectmenu-item-content">$1</span>' },
        { find: /([^\|><\(\)]+)$/g, rep: '<span class="ui-selectmenu-item-content">$1</span>' }
    ];
    for (var i in findreps) {
        newText = newText.replace(findreps[i].find, findreps[i].rep);
    }
    return newText;
}

I've removed the parts that captured and formatted (footer text). I suggest reading up on regular expressions. They are a great tool.

Jason McCreary
I have tried using this but is not working...
Lorenzo
My fault... Is working!!! thanks!
Lorenzo
No problem. Keep in mind the answer by **mykhal**. After looking at the original function, this may indeed be true. However, the resulting `<span>` set may not be what you what - i.e. class of `footer` still appears.
Jason McCreary
+1  A: 

you don't have to change anything, the code works perfectly without the footer text in parenthesis

mykhal
+1  A: 

Well, let me give you some tips to make this super easy. Change your input format to this.

This is an array of strings in JSON format.

var arr = ['John Doe', '78 West Main St Apt 3A', 'Bloomsburg, PA 12345', '(footer text)'];
var outputStr = '<span class="ui-selectmenu-item-header">' + 
       //This line concats the array elements with this text inserted
       // between each element
       arr.join('</span><span class="ui-selectmenu-item-header">') + 
'</span>';
Drew