SOLVED: Convert spaces to dashes

So I have a search box at my site (https://kennis.ga), but it doesn’t convert spaces into dashes ( - ), so you have to enter themself. This is needed because, of course, you can’t enter spaces in a URL ( https://webmaq.wixsite.com/kennis/forum/search/ SERCHTERMHERE) If you’d want to search for site builder , you’d have to enter site-builder . This is not user-friendly.

Anyone knows how to fix?
My code:

import wixLocation from 'wix-location';

export function button80_click(event) {
    wixLocation.to("https://webmaq.wixsite.com/kennis/forum/search/" + $w("#input1").value);
}; 

Thanks!

2 Likes

try:

$w("#input1").value.replace(/\s+/g, '-')
2 Likes

Ah, and if you want also get rid of unwanted spaces before and after the entire search term before you replace them by dashes:

$w("#input1").value.trim().replace(/\s+/g, '-');
2 Likes

Thanks! You are really a forum ninja, I see you in almost every post!

1 Like

I’m trying to do this in a data hook where I’m building a link using the title in a data collection.

I tried this in data.js (and of course I don’t know the proper syntax for “trim” in a data hook):

let titletrim = item.title (trim().replace(/\s+/g, '-'));

If I get the syntax right to trim it, I would then create the URL by:

 item.link = "http://www.productgrowthleaders.com/ebook/" + titletrim;

The current way I’m doing it by building it using " item.title" works, but I dislike the %20 that is used to replace the space in the URL.

For example, the title of one of our ebooks is “How to Achieve Product Success.” This data hook generates:

http://www.productgrowthleaders.com/ebook/How%20to%20Achieve%20Product%20Success

Any ideas?