1

I have a bit of a conundrum.

I have a jquery modal dialog form that is bound to a click event of a table within an accordion. On click of a row, the dialog opens up with the input fields and other elements populated with the table row data.

As part of the form, I'd like to include the accordion header text. I can extract out the text via

var activeClient = $("#strat_key_management").accordion("option", "active");
var client = $("#strat_key_management h2").eq(activeClient).text();

but there are many newlines and spaces within the text, as shown:

"\n                         CLIENT NAME FOO BAR BUZZ   \n                           \n                              \n                          \n                      "

I can remove the newlines via

client = client.replace(/\n\gm, "");

and this produces

"                           CLIENT NAME FOO BAR BUZZ   "

where the quotes show the beginning and end of the string.

How can I remove the spaces surrounding CLIENT NAME FOO BAR BUZZ but not within?

1
  • Doesn't $.trim work? Commented Jul 20, 2013 at 23:10

3 Answers 3

3

Use .replace(/^\s+|\s+$/g,"") to trim spaces from the start and end of the string.

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

Comments

1

jQuery has a built-in $.trim. In modern browsers there's String.prototype.trim:

$.trim(text); // jQuery

// OR

text.trim(); // modern browsers

Comments

1

Try using .trim():

var trimmed = client.trim();

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.