I am trying to get a Substring from a string.
Here is the slice from the code:
var inputString = '[app:abcd][class:sample][instance:value:wq-23333:site-
1.1.1.1:jhk-1][descr:endss.]'
I want to get the value after instance: to the next closing of square bracket.
So the output should be :
var outputString = 'value:wq-23333:site-1.1.1.1:jhk-1'
Note : The position where the string instance comes is not fixed and the above is a String not an Array and I am writing code in javascript.
Assuming you do have a string and just forgot to add quotes you could use a regexp /\[instance\:([^\]]+)\]/ that looks for [instance: part and captures everything until next closing bracket.
var inputString = "[app:abcd][class:sample][instance:value:wq-23333:site-1.1.1.1:jhk-1][descr:endss.]"
console.log(/\[instance\:([^\]]+)\]/.exec(inputString)[1])
Related
What I have : I have following string "/.\*n.*/"
What I want : I want to remove double quotes from above string which will look like /.\*n.*/
What I tried :
var filter = "/.\*n.*/";
var modifiedFilter = filter.replace(/"/g, "");
Somehow this code is not working. When I look at modifiedFilter in debug mode, It still shows string with double quotes "/.\*n.*/".
Is it because its a string variable and debug value shows string in double quotes?
But if it is the case, then I am passing this variable to mongodb query and there query is not working due to double quotes.
What am I missing?
// To have quotes in your string, assign it like following:
var filter = '"/.\*n.*/"';
// Your code to remove the quotes is correct.
var modifiedFilter = filter.replace(/"/g, "");
// Verify that the code worked
console.log(filter, modifiedFilter)
It is impossible to remove " from "/.\*n.*/"; in your case as it is required to create strings. It is only possible if "/.\*n.*/"; denotes the actual value of the string and in that case assignment should look like var filter = '"/.\*n.*/"', and this problem will have sense for the answerers.
Just use var modifiedFilter = filter.replace(/\"/g, "").
First have a look at the below statements executed on Node REPL.
> s = "/.\*n.*/"
'/.*n.*/'
>
> s.replace(/'/g, '')
'/.*n.*/'
>
> s = "'/.\*n.*/'"
'\'/.*n.*/\''
>
> s.replace(/'/g, '')
'/.*n.*/'
>
Now have a look at the below code.
var s = '"/.\*n.*/"'
var output = s.replace(/\"/g, "")
console.log(s) // "/.*n.*/"
console.log(output) // /.*n.*/
Your problem here is either with the query, or with attempting to treat a string primitive directly as a regular expression. Instead of trying to remove the (non-existent) speech marks, instead use the RegExp constructor to convert it into a regular expression.
var filter = "/.\*n.*/";
var modifiedFilter = new RegExp(filter).slice(1, -1);
The string needs to be sliced because the RegExp constructor accepts a regular expression without // marks.
I Would like to extract the Twitter handler names from a text string, using a regex. I believe I am almost there, except for the ">" that I am including in my output. How can I change my regex to be better, and drop the ">" from my output?
Here is an example of a text string value:
"PlaymakersZA, Absa, DiepslootMTB"
The desired output would be an array consisting of the following:
PlaymakersZA, Absa, DiepslootMTB
Here is an example of my regex:
var array = str.match(/>[a-z-_]+/ig)
Thank you!
You can use match groups in your regex to indicate the part you wish to extract.
I set up this JSFiddle to demonstrate.
Basically, you surround the part of the regex that you want to extract in parenthesis: />([a-z-_]+)/ig, save it as an object, and execute .exec() as long as there are still values. Using index 1 from the resulting array, you can find the first match group's result. Index 0 is the whole regex, and next indices would be subsequent match groups, if available.
var str = "PlaymakersZA, Absa, DiepslootMTB";
var regex = />([a-z-_]+)/ig
var array = regex.exec(str);
while (array != null) {
alert(array[1]);
array = regex.exec(str);
}
You could just strip all the HTML
var str = "PlaymakersZA, Absa, DiepslootMTB";
$handlers = str.replace(/<[^>]*>|\s/g,'').split(",");
I have the following string:
var str = '\x27';
I have no control on it, so I cannot write it as '\\x27' for example. Whenever I print it, i get:
'
since 27 is the apostrophe. When I call .length on it, it gives me 1. This is of course correct, but how can I treat it like a not escaped string and have it print literally
\x27
and give me a length of 4?
I'm not sure if you should do what you are trying to do, but this is how it works:
var s = '\x27';
var sEncoded = '\\x' + s.charCodeAt(0).toString(16);
s is a string that contains one character, the apostrophe. The character code as a hexadecimal number is 27.
After the assignment var str = '\x27';, you can't tell where the contents of str came from. There's no way to find out whether a string literal was assigned, or whether the string literal contained an escape sequence. All you have is a string containing a single apostrophe character (Unicode code point U+0027). The original assignment could have been
var str = '\x27'; // or
var str = "'"; // or
var str = String.fromCodePoint(3 * 13);
There's simply no way to tell.
That said, your question looks like an XY problem. Why are you trying to print \x27 in the first place?
I have an html element id that looks like this:
dp__1-2--1-3
I'm trying to use the JavaScript split() function to lop off and return the final '1-3'
My regex skills are poor but a bit of searching around got me to this point:
var myId = "dp__1-2--1-3";
var myIdPostFix = myId.split(/[\-\-]+/).pop();
Unfortunately that returns me only the '3'.
So my question is how do I split double hyphens but NOT single hyphens?
It's the brackets in the regular expression that keeps it from working. A set will match one of any of the characers in it, so [\-\-] is the same as [\-], i.e. matching a single hyphen.
Just remove the brackets:
var myIdPostFix = myId.split(/--/).pop();
or just use the string '--' instead of a regular expression:
var myIdPostFix = myId.split('--').pop();
split accepts a regular expression or a string as the first argument.
You were very close. You can achieve what you want with:
var myIdPostFix = myId.split("--").pop();
I tried use javascript spilt to get part of the word : new from What#a_new%20day
I tried code like this:
<script>
var word="What#a_new%20day";
var newword = word.split("%20", 1).split("_", 2);
alert(newword);
</script>
But caused:
Uncaught TypeError: Object What#a_new has no method 'split'
Maybe there have more wiser way to get the word which I need. So can anyone help me? Thanks.
split returns an array, so the second split is trying to operate on the array returned by the first, rather than a string, which causes a TypeError. You'll also want to add the correct index after the second call to split, or newword will also be an array, not the String you're expecting. Change it to:
var newword = word.split("%20", 1)[0].split("_", 2)[1];
This splits word, then splits the string at index 0 of the resulting array, and assigns the value of the string at index 1 of the new array to newword.
Regex to the rescue
var word="What#a_new%20day";
var newword = word.match(/_(.+)%/)[1];
alert(newword);
this returns the first ([1]) captured group ((...)) in the regex (_(.+)%) which is _ followed by any character (.) one or more times (+) followed by %.
the result of a split is an array, not a string. so what you need to do is
<script>
var word="What#a_new%20day";
var newword = word.split("%20", 1)[0].split("_", 2);
alert(newword);
</script>
notice the [0]
split returns an array:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split
word.split("%20", 1);
gives an array so you cannot do :
(result from above).split("_", 2);
If split is what your after, go for it, but performance wise, it would be better to do something like this:
var word="What#a_new%20day";
var newword = word.substr(word.indexOf('new'),3)
alert(newword);
Live example: http://jsfiddle.net/qJ8wM/
Split searches for all instances of %20 in the text, whereas indexOf finds the first instance, and substr is fairly cheap performance wise as well.
JsPerf stats on split vs substring (a general case): http://jsperf.com/split-vs-substring