I have a string value in a variable var string which can be
var string = '$ <input id='text'>';
OR
var string = <input id='text'>;
I need to replace anything before <input... whether it may be $ or any word. And if nothing is present need to prepend with the new value which is in var newValue.
I've tried it as follows but it works fine only if something is present before the input tag.
function replaceValue(newVal) {
amount.html(amount.html().replace(/[^\s]+/, newVal));
}
Is there any way I can prepend the value if nothing is present before input tag and restrict anyhow that <input... should not be replaced?
Cut off everything before the input and prepend the string you want to replace with.
To fix your code, you'd do:
function replaceValue(newVal) {
amount.html(newVal + amount.html().slice(amount.html().indexOf('<input')));
}
and a runnable example without jQuery can be found here: https://jsfiddle.net/rffxbfhj/
Related
I'm trying to clean up a string of text on the server side from the output generated by a wysiwyg. and while I can fix it client side, it's best to also fix this on the server side.
var string = "<p>firstline</p><p>secondline</p><p>thirdline</p><p>iframe</p><p>a</p><p>df</p><p>dsf </p><p><br></p><p>sd</p><p>f</p><p>sdf</p><p><br></p>"
var x = string.replace("<p><br></p>", "");
https://jsfiddle.net/8c0yh9r7/
the code should but doesn't get rid of the break within the paragraphs
why is that?
Use a regex with a global flag, like:
string.replace(/<p><br><\/p>/g, "");
https://jsfiddle.net/Lu2r3820/1/
When using a string only the first occurrence will be replaced.
See replace() documentation
doesn't get rid of the break within the paragraphs
Yes, it does… but only once. You have more than one paragraph containing a line break in your code.
If you want to replace it more than once, you need to use a regex and mark it as global with g.
var x = string.replace(/<p><br><\/p>/g, "");
It does replace, but only the first occurrence. If you run this afterwards, you can see the second occurrence disappearing.
var x = x.replace("<p><br></p>", "");
refer to this to replace all occurrences.
How to replace all occurrences of a string in JavaScript?
Trying to do something like making html codes usable in my forum. I want to make text hidden when wrapped with the [hidden] string and after clicking on a button the original text between the [hidden] and [/hidden] tags should be shown. I try using
var res = str.replace("[hidden]$1[/hidden]", "$1");
You need to escape the brackets, [], otherwise they will be interpreted as a character set.
var string = '[hidden]test[/hidden]';
string = string.replace(/\[hidden\](.*?)\[\/hidden\]/g, '$1');
// "test"
I have string that contains numbers and characters. I want to replace the numbers with another value that will give it a css class of someClass.
Now I got the code to detect all the numbers in the string and replace it with something else.
My question is how do I get the current number match and put it to the new string or value that will replace the original one?
Basically what I want to happen is:
for example, I have this string: 1dog3cat, I want it to get replaced with <span class="someClass">1</span>dog<span class="someClass">3</span>cat
Here's my current code:
var string_variable;
string_variable = "1FOO5,200BAR";
string_variable = string_variable.replace(/(?:\d*\.)?\d+/g, "<span class='someClass'>" + string_variable + "</span>");
alert(string_variable);
Simply remember the matched pattern using parenthesis and retrieve that value using $1 and add span tag to it.
Use this regex
string_variable = string_variable.replace(/(\d+)/g, "<span class='someClass'>$1</span>");
See DEMO
I'm not very good with RegEx, and am trying to learn. I have inherited a project which contains the following line of code:
function findCourseIdFromForm(where) {
var matchRegex = /\[course-[0-9]*\]/;
var replaceRegex = /\[|\]|[a-z]|\-/g;
return $(".cnumber", where.parent()).attr("name").match( matchRegex )[0].replace( replaceRegex,"" );
}
I'm not entirely sure what this piece of code is trying to do, but I know that it is causing issues on my page. I'm using jQuery validator and this specific component (".cnumber") is causing the validation to fail and I'm not entirely sure why, so some insight into this line is appreciated.
The .cnumber field in the HTML looks like this:
<input type="hidden" name="courses[course-0][cnumber]" class="cnumber" />
This function takes where node, gets its parent, finds .cnumber node within the parent, then takes [course-0] part and finally remove all [, ], -, and lowercase letters.
So the function returns the number that stands after [course- part, or empty string if there is no number
It strips out [, ], lowercase letters and - from the name attribute in the tag, presumably to return the course number.
It just get the course number. In you example, it return 0
I am trying to match a string in an input field using a regular expression /[1-9][a-zA-Z]/ and insert it into a <p> tag using jQuery.
I modified this example from the jQuery API docs to include the following if statement. When I type '1A' in the input field it works, however I want to exclude the rest of the string so that the <p> only includes the matched string portion.
$("input").keyup(function () {
if($(this).val().match(/[1-9][a-zA-Z]/)){
var value = $(this).val();
};
$("p").text(value);
}).keyup();
Did I explain that clearly? Could anyone point me in the right direction?
Much appreciated,
So what you are doing in the above code is that if the value of the input field matches the regular expression, you assign its value to <p> tag. Since, you want to assign the matched string to the <p> tag, you should do:
$("input").keyup(function () {
var match = $(this).val().match(/[1-9][a-zA-Z]/);
if(match){
var value = match[0]; // Your problem was here
};
$("p").text(value);
}).keyup();
The match method of a String returns an array containing the match if it passed or undefined if the match failed.