I have an probably simply question, even tho I could not find the answer for it via google or Stackoverflow.
I have a very long string which I want to store in a constant, however it looks awfull in my editor:
My objective would be to split this longer string into seperate parts inside my code, somehow like this:
As you see, JS of course does not understand that the line 8-11 should still be part of the string. How can I acomplish that?
You can escape the newlines
const str = `asödfjkdaölksdjaskldjasöldkjakldjadlkajsdEND\
BEGINNINGasdöasjkdaöslkdjasködljasdkljasdlkEND\
BEGINNINGasjköaösdjaöklsdjalkdsjaskld`
console.log(str)
Related
I'm sorry, I know the issue was addressed before, but I can't make any answer fit my problem...
I am writing a short script on google script, where I want to use the searchFile method on a folder, to look for a file whose name is stored in the string variable Name:
var theFileImLookingFor = theSourceFolder.searchFiles("title = '"+Name+"'").next();
This code works fine as long as the variable Name doesn't include quotation marks. Then, I'm stuck...
Please help me adapt my code :)
A simple but fragile solution could be to use backticks (`), usually found on the upper left of the keyboard on the same key as the tilde (~). These are sort of like quotation marks in javascript, but can also be used in ways that quotation marks can't.
Expect this solution to fail whenever the variable's value contains backticks.
Did you try to escape possible quotes ?
You probably could write a little function that take your variable "name" then escape possible quotes in it before returning it to searchFile. Or maybe with a simple "replace("'", "\'")"...
I'm very new on this and I isn't a pro on this issues!
I'm just here for request for your help because I know this community is the best for this problems!
Actually I'm using a software called "Bulk Rename Utility", it works for bulk rename files with a very useful interface, one of them is the RegEx option where you can rename files thanks to the regular expresion lenguage.
I was wonder if I can convert this code in a JavaScript code, this is because I need to modify various parameters and it could be better if I can use a JavaScript code.
As I told you, I'm very new on this. After checking some videos I learned how to use the RegEx option in this program and the codes I got was:
In the "Match" field of the program I write: (.*) 10-1[A-Z] (.*).
In the "Replace" filed of the program I write: \1_REF_\2
A then, it do the magic!
As I said, I want to know how I can convert this code to a JavaScript code because I have a lot of this codes and I want just one whole code to make a bulk process.
Thanks you so much!
Like this?
const str = 'abc 10-1G def';
const outputStr = str.replace(/(.*) 10-1[A-Z] (.*)/, '$1REF$2');
console.log(outputStr);
You just need to put $1/$2 in place of the captured groups you want in the replaced string.
To insert code, you can put the code between backticks (`) and you'll be able to type *s normally.
I know it's going to be a VERY obvious answer, but I can't find anything on how to do this.
I'm trying to unescape < and > within an HTML string
My test output string is essentially:
```php
>h2<Heading2>/h2<
```
`>h2<Heading2>/h2<`
>h2<Heading2>/h2<
So in this example we have Github flavoured Markdown, a regular code markdown snippet, and then raw text all with the same HTML tag. I want to unescape the raw tag (the third one) to actually become a link. The ideal output would be something like this.
```php
>h2<Heading2>/h2<
```
`>h2<Heading2>/h2<`
<h2>Heading2</h2>
I'm getting stuck at getting multiple > in the same line.
Current regex:
/(?:.*?(>))/
This will get the first entry.
/(?:.*?(>))/g
This one gets the second entry. I want it to be able to get EVERY entry. Then, it's just a matter of throwing the tick pieces.
/(?:```|`)(?:.*?(>)).*?(?:```|`)/gs
If you're intending on using a regular expression for this task, you can consider the following:
var r = s.replace(/((`(?:``)?)[^`]*\2)|>/g, '$1<')
.replace(/((`(?:``)?)[^`]*\2)|</g, '$1>')
.replace(/`[<>]+/g, '`');
Working Demo
I have been browsing lots of solutions, but somewhy haven't got anything to work.
I need to replace following string: "i:0#.w|dev\\tauri;" with "i:0#.w|dev\tauri;"
I have tried following JS codes to replace:
s.replace(/\\\\/g, "\\$1");
s.replace(/\\\\/g, "\\");
But have had no result. Yet following replaced my \\ with "
s.replace(/\\/g, "\"");
To be honset, then I am really confused behind this logic, it seems like there should be used \\\\ for double backshashed yet it seems to work with just \\ for two backshashes..
I need to do this for comparing if current Sharepoint user (i:0#.w|dev\tauri) is on the list.
Update:
Okay, after I used console.log();, I discovered something interesting.
Incode: var CurrentUser = "i:0#.w|dev\tauri"; and console.log(): i:0#.w|dev auri...
C# code is following:
SPWeb theSite = SPControl.GetContextWeb(Context);
SPUser theUser = theSite.CurrentUser;
return theUser.LoginName;
JavaScript strings need to be escaped so if you are getting a string literal with two back slashes, JavaScript interprets it as just one. In your string you are using to compare, you have \t, which is a tab character, when what you probably want is \\t. My guess is that wherever you are getting the current SharePoint user from, it is being properly escaped, but your compare list isn't.
Edit:
Or maybe the other way around. If you're using .NET 4+ JavaScriptStringEncode might be helpful. If you're still having problems it might help to show us how you are doing the comparison.
Here is a section of code used by CKEditor on my website:
CKEDITOR.config.IPS_BBCODE = {"acronym":{"id":"8","title":"Acronym","desc":"Allows you to make an acronym that will display a description when moused over","tag":"acronym","useoption":"1","example":"[acronym='Laugh Out Loud']lol[/acronym]", ...
If you scroll to the right just a little, you will see this:
"[acronym='Laugh Out Loud']lol[/acronym]"
I need to store all of the CKEditor code inside a javascript string, but I can't figure out how to do it because the string has both " and ' in it. See the problem? Furthermore, I don't think I can just escape the quotes because I tried doing that and the editor didn't work.
Any idea what I can do?
You might try taking the string and injecting JavaScript escape codes into it. JavaScript can essentially use any unicode value when using the format: \u#### - so, for a ' character, the code is \u0039, and for the " character, the code is \u0034.
So - you could encode your example portion of the string as:
\u0034[acronym=\u0039Laugh Out Loud\u0039]lol[/acronym]\u0034
Alternatively, you could attempt to simply escape the quotes as in:
\"[acronym=\'Laugh Out Loud\']lol[/acronym]\"
The problem here occurs when you wind up with this kind of situation:
"data:{'prop1':'back\\slash'}"
Which, when escaped in this manner, becomes:
"data:{\'prop\':\'back\\\\slash\'}\"
While this is somewhat more readable than the first version - de-serializing it can be a little tricky when going across object-spaces, such as a javascript object being passed to a C# parser which needs to deserialize into objects, then re-serialize and come back down. Both languages use \ as their escape character, and it is possible to get funky scenarios which are brain-teasers to solve.
The advantage of the \u#### method is that only JavaScript generally uses it in a typical stack - so it is pretty easy to understand what part should be unescaped by what application piece.
hmm.. you said you already tried to escape the quotes and it gave problems.
This shouldn't give problems at all, so try this:
$newstring = addslashes($oldstring);
There's no need to use Unicode escape sequences. Just surround your string with double quotes, and put a backslash before any double quotes within the string.
var x = "\"[acronym='Laugh Out Loud']lol[/acronym]\"";