How to replace string with the main string with javascript - 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"

Related

Detect line breaks (\n) while reading from a database (Javascript, HTML, firebase)

I'm trying to use a string retrieved from firebase firestore and display it on my HTML page with line breaks.
In the store, I have a string that looks like this
row1\nrow2\nrow3
When I retrieve it and try to add it to my page the \n's do not register as line breaks. I am using pre-wrap and using a test-string works fine.
let text = getString(); //retrieves a string from firebase
document.getElementById('textBox').textContent = text;
Shows this on my page:
row1\nrow2\nrow3
The following test code:
let text = 'row1\nrow2\nrow3';
document.getElementById('textBox').textContent = text;
Shows the following on the page:
row1
row2
row3
So it seems like the \n's in the string retrieved from the database are not read the same way as the \n's that are put inside the string defined directly in the Javascript code. Any idea why? And what can I do to make it read the line breaks?
Similar to what #user14063792468 said in their comment, it appears your newline characters have been escaped (i.e., converts \n to \\n) when a string is stored, so I'd try to replace any instances of \\n with \n (single backslash) and see if that works.
Here's an example of how this might look before and after the replacement:
let string = 'Newline characters\\nare in this\\nstring\\nfor sure'; // note the double slashes
document.getElementById('before').textContent = string;
let text = string.replace(/\\n/g, "\n");
document.getElementById('after').textContent = text;
<pre id="before"></pre>
<pre id="after"></pre>

javascript regexp: find anchors containing string inside tag OR inside tag

I have the following which checks for string str inside the text portion of an anchor, i.e. text containing str:
RegExp('<a.*?>.*?str.*?</a>', 'gi')
However, I need it to check inside the tag as well, i.e. for some text as well.
I tried the following but it does not work:
RegExp('<a.*?str.*?</a>', 'gi')
What syntax do I need to use for this?
Instead of using Regex, just check for the existence of the string in the innerHTML or href properties on the object:
document.querySelectorAll('a').forEach(x => {
console.log(x.innerHTML.includes('str') || x.href.indexOf('str') != -1);
})
text containing str
some text
text
some text

Javascript regex to match all characters to certain point inside content

I'm having following kind of content in my code:
something();_c.log(<ANY CONTENT HERE>);somethingElse();
Now I'd like to have a regexp that returns:
something();somethingElse();
For some reason I can't get this (easily) done. How can I achieve the desired result?
Can you use string replace with regex??
input = 'something();_c.log(<ANY CONTENT HERE>);somethingElse();'
input = input.replace(/(something\(\);).*?(somethingElse\(\);)/g, "$1$2");
Here it is capturing the two groups from your input and replacing everything excepts the two groups($1, $2).
If the something and somethingElse are unknown, and the _c.log is fixed over there, then use this one:
input = input.replace(/(\w+\(\);)_c\.log.*?(\w+\(\);)/g, "$1$2");
input = 'something();_c.log(<ANY CONTENT HERE>);somethingElse();'
input = input.replace(/_c.log\(.*?\);/g, "");
http://jsfiddle.net/2SY3w/

JavaScript string replace() not working when replacing variable

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/

Regex in JavaScript is not matching a string?

I have this JavaScript code:
var textareas = document.getElementsByTagName('textarea');
var content = textareas[0].value;
var reg = new RegExp(/^.*[[]#.+#[]].*$/mgi);
var res = content.match(reg); // always null
The content var contains a long multiline string that contains patterns like [#some text goes here#]. I tested the regex with some online testing tools and it works against the string. Using the regex in JavaScript fails though - any idea why?
Thanks!
How about this?
var content = 'foo\nhead [#some text goes here#] tail\nbar';
var reg = new RegExp(/\[#.+#\]/mgi);
var res = content.match(reg);
On execution, res contains the string '[#some text goes here#]'.
Note that I have escaped [ and ]. If they are not escaped, anything enclosed within them forms a character class.
You used [[] to escape [, which is fine, but you can't use []] to escape ] because it the first ] ends the character class in the regex. This works fine:
/^.*\[#.+#\].*$/mgi
In the case that you only want the single block and not the entire line, use:
/\[#.+#\]/mgi
This should capture the text between hashes (e.g., "some text here"):
var reg = /[^\[]*\[#([^\#]+)#\]/gims

Categories

Resources