Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 321315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:50:03+00:00 2026-05-12T08:50:03+00:00

function trim(str,options){ var string = str.replace(/^\s\s*/, ”), ws = /\s/, i = str.length, j

  • 0
function trim(str,options){
  var   string = str.replace(/^\s\s*/, ''),
        ws = /\s/,
        i = str.length,
        j = -1;
  if(options==="begin"){while(ws.test(string.charAt(++j)));return string.slice(j,i);}
  if(options==="end"){while(ws.test(string.charAt(--i)));return string.slice(j+1,i+1);}
  while(ws.test(string.charAt(--i)));while(ws.test(string.charAt(++j)));
  return string.slice(j, i + 1);
}

This function is an implementation of the Trim() method that you find in C#.

I’ve added options for removing withspace both at the beginning and end. The problem is, I can’t get it to work in a demo.

What I’ve done is this:

var a = "           zareaerar  arzare        ";
var b = "aezze          azeze      a    ";
var c = "azrazza rzrzrzrp"
var d = " aezzaeazeazeaz         azez ";

document.write('<p style="backround:#ff0000">',trim(a,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(b,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(c,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(d,"begin"),'</p>','<br />');

First of all, no background-color appears and the strings also seem to loose the spaces in the middle of the string…

Does this work with user-input only? Are strings automatically trimmed these days?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:50:04+00:00Added an answer on May 12, 2026 at 8:50 am

    1) You are not seeing a background since you have a typo: backround instead of background

    2) In HTML there is no significance for more than one whitespace. You need to use &nbsp; if you want to insert a non-breaking whitespace between words in a paragraph.

    Reference (W3 spec):

    Note that a sequence of white spaces
    between words in the source document
    may result in an entirely different
    rendered inter-word spacing (except in
    the case of the PRE element). In
    particular, user agents should
    collapse input white space sequences
    when producing output inter-word
    space
    . This can and should be done
    even in the absence of language
    information (from the lang attribute,
    the HTTP “Content-Language” header
    field (see [RFC2616], section 14.12),
    user agent settings, etc.).

    The PRE element is used for
    preformatted text, where white space
    is significant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I apply this trim function: var trimmed = str.replace(/^\s+|\s+$/g, '') ; to
I ran into this issue on accident while debugging a faulty string trim function
While looking at adding a trim function to the String prototype, I came across
Whenever I use the trim() function on a string, it works fine with Chrome
I'm trying to write simple function trim space in the string in ansi C.
I have created simple function create function TRIM(@data varchar(20)) returns varchar(100) as begin declare
heres my code: function toAscii($str, $replace=array(), $delimiter='-') { if( !empty($replace) ) { $str =
I've just written a limitChars() function. var limitChars = function(str, limit, endChar, preserveWord) {
I have code like this: function search_keyword(){ $keyword = $this->db->escape_like_str(trim($_POST['keyword'])); $sql = ( SELECT
http://php.net/manual/en/function.trim.php The manual entry does say that I can remove any specified leading or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.