When i have string which consists of a single line, replace works just fine.
As soon as i type in some text into text area and press enter as for new line, replace won't work anymore.
var currentValue = $('#service-field').val();
$('#service-field').val(currentValue.replace("particular string",""));
What should I do?
Try this to make sure you capture all occurrences, and not just the ones on the first line:
$('#service-field').val(currentValue.replace(/particular string/g, ""));
Or with a variable:
var t = "particular string";
$('#service-field').val(currentValue.replace(eval("/" + t + "/g"), ""));
Related
How to convert the following using js regex replace or something:
var text = "line1<br>line2<br>line3<br>line4<br>"
...js regex...
so that text becomes:
text = "line1<br>line2<br>line3<br>";
of course the number of br varies, and each line length varies too
Removing just the last line without regular expressions:
var text = "line1<br>line2<br>line3<br>line4<br>";
var pieces = text.split('<br>');
var newText = pieces.splice(0, pieces.length-2).join('<br>') + '<br>';
Note that this code makes at least three important assumptions:
The separator is always <br> and not, say, <br /> or <BR>.
The input string always ends with a <br>.
The result string always ends with <br>, even if it's otherwise empty! (For example, if the input string is line1<br> then the output will be <br>.
Here is one that I am posing just because I took the time to make it. It works but needs index and null checks and such.
var text = "line1<br>line2<br>line3<br>line4<br>";
var newText = text.substring(0, text.substring(0, text.length - 4).lastIndexOf("<br>"));
I am trying to replace some letters in a textarea with some text from an input box.
The problem that I am facing is that it replaces all the values that are specified in CN.
For example, if CN is "A", then "A" will be replaced as well as "A" in "Apple". I want to replace only "A". I have tried using chordName = CN + " ", but that replaces values only where CN is in the middle of a sentence, not at the beginning of a line.
This is my code:
function doit(CN, SC) {
var rawLyrics = document.getElementById("ttbox").value;
var chordName = CN;
var spnCode = SC;
var res = rawLyrics.replace(new RegExp(chordName, 'g'), spnCode);
results.innerText = res;
}
I also have the problem where "results" textarea gets refreshed with the replaced words rather than changing only the required words i.e, I am using the above code to replace different words in a single block of text, so once I replace one word, and move on to the next, the previously replaced word comes back to it's previous state.
What am I doing wrong? Any solutions?
I have this little problem with jQuery. I want to remove an specific text from textarea. Check my codes:
Textarea values:
aa
a
aaa
i tried this:
$("#id_list").val($("#id_list").val().replace("a", " "));
The codes above only works if the text in each line is unique with no matching characters from other lines.
Now the problem is the codes above removes the first letter from aa, instead of removing the second line a. How can I get it to work in replace/removing an exact word on a line from textarea? Any help would be much appreciated.
Use word boundary.
Do this:
$("#id_list").val($("#id_list").val().replace(/\ba\b/g, " "));
That will replace only a
If you want to replace just one time, remove g from my regex.
If you want to use strings stored in a variable, do this:
var word = "a";
var regex = new RegExp("\\b"+word+"\\b","g");
$("#id_list").val($("#id_list").val().replace(regex, " "));
Just use replace(/a/g, " ")) instead. the /g flag means you search globally for the "a" letter. Without it you just replace the first occurrence.
You need to use regex replace
replace(/a/g, " "))
I'm working on an autocomplete component that highlights all ocurrences of searched text. What I do is explode the input text by words, and wrap every ocurrence of those words into a
My code looks like this
inputText = 'marriott st';
text = "Marriott east side";
textSearch = inputText.split(' ');
for (var i in textSearch) {
var regexSearch = new RegExp('(?!<\/?strong>)' + textSearch[i]), "i");
var textReplaced = regexSearch.exec(text);
text = text.replace(regexSearch, '< strong>' + textReplaced + '< /strong>');
}
For example, given the result: "marriott east side"
And the input text: "marriott st"
I should get
<strong>marriot< /strong > ea < strong >st < /strong > side
And i'm getting
<<strong>st</strong>rong>marriot</<strong>st </strong>rong>ea<<strong>st</strong> rong>s</strong> side
Any ideas how can I improve my regex, in order to avoid ocurrences inside the html tags? Thanks
/(?!<\/?strong>)st/
I would process the string in one pass. You can create one regular expression out of the search string:
var search_pattern = '(' + inputText.replace(/\s+/g, '|') + ')';
// `search_pattern` is now `(marriot|st)`
text = text.replace(RegExp(search_pattern, 'gi'), '<strong>$1</strong>');
DEMO
You could even split the search string first, sort the words by length and combine them, to give a higher precedence to longer matches.
You definitely should escape special regex characters inside the string: How to escape regular expression special characters using javascript?.
Before each search, I suggest getting (or saving) the original search string to work on each time. For example, in your current case that means you could replace all '<strong>' and '</strong>' tags with ''. This will help keep your regEx simple, especially if you decide to add other html tags and formatting in the future.
I'm trying to create a JavaScript script for highlighting certain text on a page. Right now I'm having issues trying to replace text (from the body html) with other text. I want to replace all instances of each item in the array highlights with some other text.
The code that I'm using is:
var responseText = server.responseText;
var highlights = responseText.split("\n");
var text = document.body.innerHTML;
for (i in highlights) {
if (highlights[i].length > 1) {
var exp = new RegExp(highlights[i], "g");
console.log(exp);
console.log(highlights[i]);
text = text.replace(exp, "XXXXXXXXXXX");
}
}
document.body.innerHTML = text;
Currently, I am getting the correct value printouts for highlights[i] and I think I am for the regular expression exp; if highlights[i] is 'Remember', then the printout I'm getting for exp is '/Remember/g' (without the quotation marks) -- but it's not replacing the word 'Remember' on the page. 'And if I replace highlights[i] in the new RegExp() with simply the string "Remember" it works correctly. Any ideas on what's wrong?
EDIT:
I solved the problem! When creating the RegExp() I passed in highlights[i].trim() instead of just highlights[i] to get rid of whitespace at the beginning/end and it appears to be working now.
There is some problem with your multiline server.responseText .
I replaced the input with spaces instead of newlines, and all the replacements work fine :
http://jsfiddle.net/XTdgJ/1/