Javascript - How to replace a simple string

Related searches

I have tried every variation to replace the string, "sub events" with "selected. I haven't touched "sub events" yet but have affected everything around it.

This hid everything after it:

<script type="text/javascript">
$("div[class$=registrationDetails] > ul > li").text(function(){
     var str = $(this).text("and the sub events").str.replace("sub events", "selected");

I tried the W3 School method, which I also found on another StackOverflow post, and it did nothing. This is the most basic form I tried with this method.

<script type="text/javascript">
var str1 = "and the sub events";
var str2 = str1.replace("sub events", "selected");

It seems so close yet won't just replace as it should. Thanks.

First revise how you're returning your value in the callback and replace as so. This is all assuming that the <li /> has the text you're looking to replace.

  $("div[class$=registrationDetails] > ul > li").text(function() {
    return this.innerText.replace("sub events", "selected");

JavaScript String replace() Method, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� 2.2 replace () with a string. If the first argument search of string.replace (search, replaceWith) is a string, then the method replaces only the first occurrence of search: const search = ' '; const replace = '-'; const result = 'duck duck go'.replace(search, replace); result; // => 'duck-duck go'.

This thing worked for me.

var str1 = "and the sub events";

str1 = str1.replace("sub events", "selected")


Output -

[Running] node "..\test.js" and the selected

JavaScript String Replace ←, Replacing text in a string is a very common operation, and thank's to the replace method available on the String prototype, it's very easy to do. The simplest� This tutorial help to understand javascript string replace with an example.The javascript has a replace () method that use to search and replace into string.You can search a constant string or regular expression.This method return the new string which has replaced word with in target string.

Here is a way to do what you want with a each loop. Used a if statement to look to see if the match is there

$(function() {
  // select the class with a dot, not attribute selector
  $("div.registrationDetails > ul > li").each(
    function() {
      /// read text
      var text = $(this).text()
      // check if text is there
      if (text.includes('sub events')) {
        // if it is replace
        var str = text.replace('sub events', 'selected')
        //update the element
      // another change
      } else if (text.includes('selected')) { 
        var str = text.replace('selected', 'sub events')
<script src=""></script>

<div class="registrationDetails">
    <li>foo bar sub events</li>
    <li>other selected</li>

String.prototype.replace(), The string.replace() is an inbuilt function in JavaScript which is used to replace a part of the given string with some another string or a regular� I found the way to replace the text and maintain the html list formatting and wanted to note it for the next person. <script type="text/javascript"> $('div.registrationDetails > ul > li').html(function(i, htm){ return htm.replace(/sub events/, 'selected'); }); </script> – ccllraabla Feb 7 at 17:13

I found the script to replace the text and maintain the html list formatting.

<script type="text/javascript">
$('div.registrationDetails > ul > li').html(function(i, htm){
    return htm.replace(/sub events/, 'selected');

JavaScript, There's no easy way to replace all string occurrences in JavaScript. Java, which had served an inspiration for JavaScript in the first days, has� In JavaScript, replace () is a string method that is used to replace occurrences of a specified string or regular expression with a replacement string. Because the replace () method is a method of the String object, it must be invoked through a particular instance of the String class.

3 Ways To Replace All String Occurrences in JavaScript, A protip by avsej about javascript. String.prototype.format = function() { a = this; for (k in arguments) { a = a.replace("{" + k + "}", arguments[k]) } return a }

Using String Methods In JavaScript, regular expressions are often used with the two string methods: search () and replace (). The search () method uses an expression to search for a match, and returns the position of the match. The replace () method returns a modified string where the pattern is replaced.

JavaScript has a very simple and straightforward option for string replacement, but it has some shortcomings that require modification or a completely different alternative to achieve a “full

  • Do not use [class$=registrationDetails] when looking for an element with a class. Use div.registrationDetails
  • Your problem is the use of jQuery is wrong. Is the text in the page already?
  • What result are you expecting after you have replaced the string?
  • Your guidance is appreciated! I switched to use the div.registrationDetails and that does target the correct section. I have another script on the page removing some dates just below the place where I want the outcome to say "selected" instead of "sub events". The page with no changes to it looks like this:
  • <div class="panel-body registrationDetails"> will be registered for <strong> Hospitality Volunteer Oportunities</strong> <ul style="list-style: none;"> <li>and the sub events <ul> <li>6/30/2020 9:00 AM - 10:00 AM : Volunteer at North Campus</li> <li>6/30/2020 1:00 PM - 2:00 PM : Volunteer at South Campus</li> </ul> </li> </ul>
  • The top suggestion you provided came very close to working. I have generated the same outcome with a similar script. The issue is that the list below "sub events" loses its formatting and the options selected end up on the same line with the replaced text "selected". I am hoping to preserve the bulleted list under the "selected" line. I can't grab "sub events" and preserve the formatting.
  • your solution worked to replace the text and straightened me out on selecting the class. I found the way to replace the text and maintain the html list formatting and wanted to note it for the next person. <script type="text/javascript"> $('div.registrationDetails > ul > li').html(function(i, htm){ return htm.replace(/sub events/, 'selected'); }); </script>