Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

317
Views
modify user input in javascript

I need to take a user input, in this case as a search word. But when sending this input to an API, the string must not contain any spaces. I tried .trim but this seems to only remove any spaces before and/or after the string. Essentially, I need to force any amount of words other than one together, any thoughts on how this could be achieved would be greatly appreciated.

function getSearch(){
var welcomeMsg = document.getElementById('result');
var search_term = document.getElementById('floatingInput');

<input type="text" class="form-control" id="floatingInput">
  <input type="button" value="click">
  <label for="floatingInput">Song title / artist name</label>
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use String.prototype.replace() for this, for example:

> 'foo bar'.replace(' ', '')
'foobar'

If there are expected to be many spaces, you can use a global regular expression:

> 'foo bar baz otherfoo'.replace(/ /g, '')
'foobarbazotherfoo'

Finally, if you still want to see the word boundaries as suggested by @JoeKincognito, you can update the second argument from an empty space '' to the separator of your choice, for example:

> 'foo bar baz otherfoo'.replace(/ /g, '-')
'foo-bar-baz-otherfoo'

This will allow you to separate the search terms later if necessary, and will allow you to differentiate between users searching for e.g., butter and fingers from users searching for butterfingers

over 4 years ago · Santiago Trujillo Report

0

string.replace() method supports regex expression:

xxx.replace(/\s+/g, '') //xxx is an example string.
\s: matches any whitespace symbol
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!