简体   繁体   English

如何替换字符串中的字符

[英]How to replace characters in a string

I have this script used with a form (Credits to: Luka [./users/6634591]) 我将此脚本与表单一起使用(Credits to:Luka [./users/6634591])

<div class="center">
<form id="search">
  <input type="text" id="keyword" />
  <button type="submit">Search</button>
</form>
</div>

<script>
document.querySelector("#search").addEventListener("submit", urlMaker)

function urlMaker(event) {
let keyword = document.querySelector("#keyword").value;
let base = "https://www.example.com/list.php?q=";
let ending = "&dhd=1&hdd=low&dtt=list";
let url;

  let url;
  event.preventDefault();
  url = base + keyword + ending;
  window.location.href = url;
}

What I would like to do is parse the url string to replace certain characters and use like url.replace = Parsed value 我想要做的是解析url字符串以替换某些字符并使用像url.replace = Parsed value

I am pretty skilled at building regexes. 我非常擅长构建正则表达式。 Would it be possible to parse it through a regex function at that stage? 是否有可能在该阶段通过正则表达式函数进行解析? PHP is not an option as I do want this to work without apache PHP不是一个选项,因为我希望这个没有apache

Like always I have googled my eyes bleeding trying to find an answer before posting. 像往常一样,我一直在搜索我的眼睛,试图在发布之前找到答案。

Thank you for any input! 谢谢你的任何输入!

您正在寻找接受正则表达式模式的String.replace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM