I have a string like #'test'
and I have to get only test word without #' '
how can I match them with regular expression at javascript?
try the regex below:
#\'([^']*)\'
get group(1)
and if your language supports look-behind,( e.g. python, perl, java...):
(?<=#\')[^']*
should work too without grouping.
oh, just saw you edit your question, as far as I know, js doesn't support look-behind, then you could take the option 1.
var firsName= "#'test'"
var objName = firsName .replace('#', "'");
lastName=objName.replace(/[']/g,'');
I fixed it like that. I dont know is it best way or not. thanks for helping
Related
My current code is:
var user_pattern = this.settings.tag;
user_pattern = user_pattern.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); // escape regex
var pattern = new RegExp(user_pattern.replace(/%USERNAME%/i, "(\\S+)"), "ig");
Where this.settings.tag is a string such as "[user=%USERNAME%]" or "#%USERNAME%". The code uses pattern.exec(str) to find any username in the corresponding tag and works perfectly fine. For example, if str = "Hello, [user=test]" then pattern.exec(str) will find test.
This works fine, but I want to be able to stop it from matching if the string is wrapped in [nocode][/nocode] tags. For example, if str = "[nocode]Hello, [user=test], how are you?[/nocode]" thenpattern.exec(str)` should not match anything.
I'm not quite sure where to start. I tried using a (?![nocode]) before and after the pattern, but to no avail. Any help would be great.
I would just test if the string starts with [nocode] first:
/^\[nocode\]/.test('[nocode]');
Then simply do not process it.
Maybe filter out [nocode] before trying to find the username(s)?
pattern.exec(str.replace(/\[nocode\](.*)\[\/nocode\]/g,''));
I know this isn't exactly what you asked for because now you have to use two separate regular expressions, however code readability is important too and doing it this way is definitely better in that aspect. Hope this helps ๐
JSFiddle: http://jsfiddle.net/1f485Lda/1/
It's based on this: Regular Expression to get a string between two strings in Javascript
the string looks something like below:
/1/2/3/4 however I want to replace this with ?1=2&3=4.
I am planning to use REReplace in ColdFusion.
Could you suggest me a regex for this ?I also thought of using loops but stuck either way...
Thanks in Advance
A bit cumbersome without making it more manageable using a loop as #Leigh suggested; but you can use the following on string inputs that contain even occurrences of n/m in the format you described:
var s = "/1/2/3/4/5/6";
s.replace(/^\//,'?').replace(/(\d+)\/(\d+)/g,'$1=$2').replace(/\//g,'&')
// => "?1=2&3=4&5=6"
I'm working on a search function that can show partial result according to your search variable.
For example we are looking for a word "abcde".
We have records of strings like
"ioqwepasaea" "uhabcsdwe" "ewqabcde" "abcfeqs" "cdeeqwee"
So the results should show "uh*abc*sdwe" - "ewq*abcde*"- "*abc*feqs" - "*cde*eqwee"
Is this possible using Jquery? Thank you in advance!!!
Searching for substrings inside strings is done by using regular expressions.
Regular expressions (RegEx) can be used in javascript as well as other languages. It's universal.
RegEx is broadly used for datavalidation but can be used for searching for substrings as well.
There is a lot of guides on the internet on this topic.
I found one for you. take a look :)
http://www.javascriptkit.com/javatutors/re.shtml
As I understand your question, You want to search relating string to your database or etc. You can do it this so many ways. But "jQuery String Functions" are very helpful your solution and also look at Regular Expression. Please try it code below for inspiration related to "string functions".
var myRecords:Array = ["xjavascriptfasf","cavascript","abascript"];
var searchValue = "javascript";
for(var i =0; i<myRecords.length; i++) {
console.log(myRecords[i].search(searchValue));
}
output should be like this :
1
-1
-1
Thats means in your database and user search matched and returned it correct string.
Also please take a look at this "jquery string functions" http://www.jquerybyexample.net/2012/06/jquery-string-functions.html
I hope this is help your trouble.
I'm struggling a little bit with a JavaScript regex statement - and I can't quite see what's wrong. I've tested in online tools and they suggest it should work so I'm assuming there's something different between C# regex that I'm used to and JavaScript.
The string I'm working with is quite simple:
[a] + [b]
The regex match I'm trying to use is:
/[(?<name>[a-zA-Z0-9])/]
I'm trying to replace the value with the following:
viewModel.$1.control.value()
Which should leave me with:
viewModel.a.control.value() + viewModel.b.control.value()
Unfortunately I'm always getting my inital value printed, suggesting my matching isn't working but I can't see why. The only obvious thing I tried was switching the escaping of the square brackets between forward and backslash.
Can anyone suggest what else might be wrong?
There is no named groups in Javascript regex. Use this:
var s = '[a] + [b]';
repl = s.replace(/\[([a-zA-Z0-9])\]/g, 'viewModel.$1.control.value()');
//=> "viewModel.a.control.value() + viewModel.b.control.value()"
Also you need to escape [ and ] in order to match them in a regex.
I am trying to use John Gruber's URL regex in Javascript but NetBeans keeps telling me there is a syntax error and illegal errors:
var patt = "/(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])
|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]
{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|
(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|
(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:
'".,<>?ยซยปโโโโ]))/";
Anyone know how to solve this?
As others have said, it's the double quote. But alternatively, you can just write the regexp as a literal in javascript (but then you need to escape the forward slashes in lines 1 and 3 instead).
var regexp = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?ยซยปโโโโ]))/i;
I also moved the case-insensitive modifier to the end. Just because. (edit: Well, not just "because" - see Alan Moore's comment below)
Note: Whether you use a literal or a string, it has to be on 1 line.
put the whole expression in one line, and remove the quotes at the start and end so it looks like this var patt = /the-long-patttern/;, netbeans will still complain, but the browsers won't and thats what matters.
You should write it like this in NetBeans:
"(?i)\\b((?:[a-z][\\w-]+:(?:\\/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]"
+ "+[.][a-z]{2,4}\\/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))"
+ "+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?ยซยปโโโโ]))";