The string that i have is
[<span class="link" nr="28202" onclick="javascript:openAnlaggning('/kund/Bridge/Main.nsf/AnlOkatSamtligaFaltCopy/045C9DDFDC7AB308C1257C1B002E11F1?OpenDocument&urval=1');" >Alingsås Järnvägsstation</span>]
The logic is to check if there is a '[' at the start of the string and if it is present then take the value between the square brackets. In the above string what i would like to get as output is
<span class="link" nr="28202" onclick="javascript:openAnlaggning('/kund/Bridge/Main.nsf/AnlOkatSamtligaFaltCopy/045C9DDFDC7AB308C1257C1B002E11F1?OpenDocument&urval=1');" >Alingsås Järnvägsstation</span>
I tried with this
var out = value.match('/\[(.*)\]/i');
I tried it on scriptular.com,and i do get a match.
Thanks in advance.
Remove the quotes to make the argument a real regular expression literal:
// -------------------v --------v
var out = value.match(/\[(.*)\]/i);
You could use the below regex to get the values inside [] braces,
\[([^\]]*)\]
DEMO
Your regex \[(.*)\] will not work if there is another ] at the last. See the demo.
For this you have to make your regex to do a non-greedy match by adding ? quantifier next to *,
\[(.*?)\]
DEMO
Related
I have to find some key strings and surrond them with quote if they are not:
`[aa,bb,cc, "aa","bb","cc"]`.replace(/[^"](aa|bb)[^"]/g, `"$1"`)
expected:
"["aa","bb",cc, "aa","bb","cc"]"
but I got this:
""aa""bb",cc, "aa","bb","cc"]"
What was happend with '[' and comma ','?
You need to capture [^"] too and use back reference while replacing, as you're not capturing them and replacing only the matched value with the captured group so you end up loosing the value matched by [^"]
let final = `[aa,bb,cc "aa","bb","cc"]`.replace(/([^"])(aa|bb)(?!")/g, `$1"$2"`)
console.log(final)
[aa,bb,cc "aa","bb","cc"].replace(/([^"])(\w\w)(?!")/g, $1"$2")
Hi I want to escape single quote within another string.
I have the following string:
'I'm a javascript programmer'
In the above string , I need to escape single quote
and the expected output is:
'I\'m a javascript programmer'
I required this to handle in eval() in javascript.
The String would be like this...
"[['string's one','string two','string's three']]"
How to solve this. Thanks in advance...
This can do the trick:
var str = "'I'm a js programer.'";
str.replace(/(\w)'(\w)/g, "$1\\\'$2");
var s = "my string's"
s = s.replace(/'/g, "\\'");
The proper way to escape quotes in an html string would be with a character entity.
'I'm a javascript programmer'
The same goes for double quotes:
'"I'm a javascript programmer"'
You can try lookahead assertions to achieve the desired effect:
var str = "'I'm a javascript's programmer'";
str = str.replace(/(?!^)'(?!$)/g, "\\'");
(Fiddle). Unlike Shimon's answer, this can also deal with double single quotes ('').
Negative lookahead assertion (?! )doesn't do any matching by itself but it ensures that the asserted expression doesn't occur at the given position (i.e. start of string ^ doesn't occur before the quote and end of string $ doesn't occur after the quote).
I have a url like http://www.somedotcom.com/all/~childrens-day/pr?sid=all.
I want to extract childrens-day. How to get that? Right now I am doing it like this
url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all"
url.match('~.+\/');
But what I am getting is ["~childrens-day/"].
Is there a (definitely there would be) short and sweet way to get the above text without ["~ and /"] i.e just childrens-day.
Thanks
You could use a negated character class and a capture group ( ) and refer to capture group #1. The caret (^) inside of a character class [ ] is considered the negation operator.
var url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all";
var result = url.match(/~([^~]+)\//);
console.log(result[1]); // "childrens-day"
See Working demo
Note: If you have many url's inside of a string you may want to add the ? quantifier for a non greedy match.
var result = url.match(/~([^~]+?)\//);
Like so:
var url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all"
var matches = url.match(/~(.+?)\//);
console.log(matches[1]);
Working example: http://regex101.com/r/xU4nZ6
Note that your regular expression wasn't actually properly delimited either, not sure how you got the result you did.
Use non-capturing groups with a captured group then access the [1] element of the matches array:
(?:~)(.+)(?:/)
Keep in mind that you will need to escape your / if using it also as your RegEx delimiter.
Yes, it is.
url = "http://www.somedotcom.com/all/~childrens-day/pr?sid=all";
url.match('~(.+)\/')[1];
Just wrap what you need into parenteses group. No more modifications into your code is needed.
References: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
You could just do a string replace.
url.replace('~', '');
url.replace('/', '');
http://www.w3schools.com/jsref/jsref_replace.asp
I have a textArea. I am trying to split each string from a paragraph, which has proper grammar based punctuation delimiters like ,.!? or more if any.
I am trying to achieve this using Javascript. I am trying to get all such strings in that using the regular expression as in this answer
But here, in javascript for me it's not working. Here's my code snippet for more clarity
$('#split').click(function(){
var textAreaContent = $('#textArea').val();
//split the string i.e.., textArea content
var splittedArray = textAreaContent.split("\\W+");
alert("Splitted Array is "+splittedArray);
var lengthOfsplittedArray = splittedArray.length;
alert('lengthOfText '+lengthOfsplittedArray);
});
Since its unable to split, its always showing length as 1. What could be the apt regular expression here.
The regular expression shouldn't differ between Java and JavaScript, but the .split() method in Java accepts a regular expression string. If you want to use a regular expression in JavaScript, you need to create one...like so:
.split(/\W+/)
DEMO: http://jsfiddle.net/s3B5J/
Notice the / and / to create a regular expression literal. The Java version needed two "\" because it was enclosed in a string.
Reference:
https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
You can try this
textAreaContent.split(/\W+/);
\W+ : Matches any character that is not a word character (alphanumeric & underscore).
so it counts except alphanumerics and underscore! if you dont need to split " " (space) then you can use;
var splittedArray = textAreaContent.split("/\n+/");
How can I write regular expression to remove double dash -- into single dash - and if string start or end with dash replace with empty string.
var oldString = "abc--xyz--"
var filtered = oldStringt.replace(???????);
Sample Input >>>> Output
abc--xyz-- >>>>> abc-xyz
abc---xyz-123 >>>>> abc-xyz-123
--abc-xyz-123 >>>>> abc-xyz-123
How about chaining replaces:
str.replace(/[-]+/g, '-').replace(/[-]+$/g, '').replace(/^[-]+/g, '')
Fiddle here.
oldString.replace(/-+/g,"-").replace(/(^-+)|(-+$)/g,"");
Here is a single regexp that should work:
oldString.replace(/^-+|-+$|(-)+/g, '$1')
Tests: http://jsfiddle.net/kd9g3/
Now, I know you specifically asked for regexp, but many replaces like this one can be done using arrays as well (and sometimes they are faster):
oldString.split(/-+/).filter(function(e){return !!e}).join('-')