'jQuery find multiple occurrences of a string in a string
I'm trying to convert a string with a known pattern to a string with HTML Select and Options. All the Select Options are CSV between [start_option]CSV[stop_option]. There may be multiple occurrences in one string. I need to convert them all. Below is my code example.
jQuery(document).ready(function() {
var string_one = 'On [start_option]monday,tuesday,wednesday[stop_option] the [start_option]dog,cat,pig,cow[stop_option] went up the hill and hit the [start_option]red,green,pink,blue[stop_option] ball.';
function createDropDownList(input_string) {
/////////////////////
// code to find and create <select id="" name=""><option valur=""></option></select>
// for each occurance in the string
////////////////////
}
var string_one_output = createDropDownList(string_one);
////////////////
// This would be the expected output of the function
// string_one_output = 'On <select id="select0" name="select0><option value="Monday">Monday</option><option value="Tuesday">Tuesday</option><option value="Wednesday">Wednesday</option></select>the <select id="select1" name="select1><option value="Dog">Dog</option><option value="Cat">Cat</option><option value="Pig">Pig</option><option value="Cow">cow</option></select> went up the hill and hit the <select id="select2" name="select2><option value="Red">Red</option><option value="Green">Green</option><option value="Pink">Pink</option><option value="Blue">Blue</option></select> ball.';
///////////////
$("#div_one").html(string_one_output);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="div_one" name="div_one" class="fancy_div"></div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
