omit final blank character in a search

Dear all,

I’ve got a database search working well for a glossary type website, however, if someone accidentally adds a space after the word they are searching, it brings no result, in spite the fact the word is there.

As as example, if I search for “desejo” I will get results. But if the user accidentally adds a space, like this "desejo " or pastes it in, it brings no results, because the “space” is not in the database.

Can someone direct me to right code to able to omit the final character if it is blank (and punctuation too) .

here is my current code:

import wixData from ‘wix-data’ ;

$w . onReady ( function () {
//TODO: import wixData from ‘wix-data’;
});

// search 1 ////////////////////////////////////////////

export function searchButton1_click ( ) {
search1 (); //
}

export function input1_keyPress ( event , $w ) {
if ( event . key === ‘Enter’ ){
search1 ();
}
}
function search1 ( ) {
wixData . query ( “glossary” )
. contains ( “iast” , $w ( “#input1” ). value )
. ascending ( “iast” ))
. limit ( 1000 )

. find () // Run the query

. then ( res => {
// Set the table data to be the results of the query

$w ( "#table1" ). rows  =  res . items       
**if** ( res . items . length  >  0 ){ 
    $w ( "#table1" ). rows  =  res . items ; 
    $w ( "#noResult" ). collapse (); //this is a texbox for "No results found" message 
}  **else**  { 
    $w ( "#noResult" ). expand (); 

}
})
. catch ( ( error ) => {
let errorMsg = error . message ;
let code = error . code ;
});
}

Thank you in advance!