2

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

2
  • what is your desired result? you want to have empty <span class="ui-selectmenu-item-footer" /> ? Commented Oct 18, 2010 at 1:39
  • please have a look to my edit at the end of the question Commented Oct 18, 2010 at 1:42

4 Answers 4

1

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.

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

1 Comment

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.
1

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

Comments

1

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>';

Comments

0

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

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.