How to get value without brackets using JQuery plugin inputmask? - javascript

There is the following code:
$("#ce_clientphone").inputmask("+9(999)9999999")
...
console.log($('#ce_clientphone').unmask())
As you can see I try to get value from '#ce_clientphone' without brackets. How can I do it? I need to allow user to input valid phone, but to save it in the database I need to remove brackets. Thanks in advance.

You could do regular expressions and replace to remove the brackets
"+9(999)9999999".replace(/[()]/g,'');

var str="+9(999)9999999";
alert(str.replace(/[()]/g,''));
DEMO

Try this:
$('#ce_clientphone').unmask().replace(/[()]/g, '');
For example:
"+9(999)9999999".replace(/[()]/g, '');
Returns:
"+99999999999"
So, how does this work?
We're using a regex to replace the brackets:
/ // Start of regex
[ // Start of character group
() // match either of these characters in the group (So match `(` OR `)`)
] // End of character group
/ // End of regex
g // `Global` flag. This means the regex won't stop searching after the first replace

Related

How to delete brackets after a special letter in regex

Hi I am having problem while trying to remove these square brackets.
I figured out how to find square brackets but I need to find square brackets only if it starts with # like this,
by the way I am using .replace to remove them in javascript, Not sure if it is going to help to find the answer.
#[john_doe]
The result must be #john_doe.
I dont want to remove other brackets which is like that,
[something written here]
Here is the link of the regex
You need a regular expression replace solution like
text = text.replace(/#\[([^\][]*)]/g, "#$1")
See the regex demo.
Pattern details
#\[ - a #[ text
([^\][]*) - Group 1 ($1): any zero or more chars other than [ and ] (the ] is special inside character classes (in ECMAScript regex standard, even at the start position) and need escaping)
] - a ] char.
See the JavaScript demo:
let text = '#[john_doe] and [something written here]';
text = text.replace(/#\[([^\][]*)]/g, "#$1");
console.log(text);
You can use Regular expression /#\[/g:
const texts = ['#[john_doe]', '[something written here]']
texts.forEach(t => {
// Match the # character followed by an opening square bracket [
const result = t.replace(/#\[/g, '#')
console.log(result)
})
let name = "#[something_here]"
name.replace(/(\[|\])/, "")

What RegEx would clean up this set of inputs?

I'm trying to figure out a RegEx that would match the following:
.../string-with-no-spaces -> string-with-no-spaces
or
string-with-no-spaces:... -> string-with-no-spaces
or
.../string-with-no-spaces:... -> string-with-no-spaces
where ... can be anything in these example strings:
example.com:8080/string-with-no-spaces:latest
string-with-no-spaces:latest
example.com:8080/string-with-no-spaces
string-with-no-spaces
and a bonus would be
http://example.com:8080/string-with-no-spaces:latest
and all would match string-with-no-spaces.
Is it possible for a single RegEx to cover all those cases?
So far I've gotten as far as /\/.+(?=:)/ but that not only includes the slash, but only works for case 3. Any ideas?
Edit: Also I should mention that I'm using Node.js, so ideally the solution should pass all of these: https://jsfiddle.net/ys0znLef/
How about:
(?:.*/)?([^/:\s]+)(?::.*|$)
Consider the following solution using specific regex pattern and String.match function:
var re = /(?:[/]|^)([^/:.]+?)(?:[:][^/]|$)/,
// (?:[/]|^) - passive group, checks if the needed string is preceded by '/' or is at start of the text
// (?:[:][^/]|$) - passive group, checks if the needed string is followed by ':' or is at the end of the text
searchString = function(str){
var result = str.match(re);
return result[1];
};
console.log(searchString("example.com:8080/string-with-no-spaces"));
console.log(searchString("string-with-no-spaces:latest"));
console.log(searchString("string-with-no-spaces"));
console.log(searchString("http://example.com:8080/string-with-no-spaces:latest"));
The output for all the cases above will be string-with-no-spaces
Here's the expression I've got... just trying to tweak to use the slash but not include it.
Updated result works in JS
\S([a-zA-Z0-9.:/\-]+)\S
//works on regexr, regex storm, & regex101 - tested with a local html file to confirm JS matches strings
var re = /\S([a-zA-Z0-9.:/\-]+)\S/;

How to get the 1st character after a pattern using regex?

I'm trying to get the first character after the pattern.
i.e.
border-top-color
padding-top
/-[a-z]/g
selects:
border[-t]op[-c]olor
padding[-t]op
I want to select:
border-[t]op-[c]olor
padding-[t]op
How do you just get the first character after a selected pattern?
Example Here! :)
To get the t after border-, you usally match with this kind of regex:
border-(.)
You can then extract the submatch:
var characterAfter = str.match(/border-(.)/)[1];
match returns an array with the whole match as first element, and the submatches in the following positions.
To get an array of all the caracters following a dash, use
var charactersAfter = str.match(/-(.)/g).map(function(s){ return s.slice(1) })
Just use a capturing group:
"border-top-color".replace(/-([a-z])/g, "-[$1]")
Result:
"border-[t]op-[c]olor"
You can use submatching like dystroy said or simply use lookbehind to match it:
/(?<=-)./

Javascript Regex - Get All string that has square brackets []

I have string data below:
var data = "somestring[a=0]what[b-c=twelve]----[def=one-2]test"
I need to get all strings that contain square brackets []. This is the result that I want.
["[a=0]", "[b-c=twelve]", "[def=one-2]"]
I've tried using regex /\[(.*?)\]/, but what I've got is an only the first array element is correct, the next elements are basically the same value but without the square brackets.
data.match(/\[(.*?)\]/);
// result => ["[a=0]", "a=0"]
What regexp should I use to achieve the result that I want? Thank you in advance.
You want to use the g (global) modifier to find all matches. Since the brackets are included in the match result you don't need to use a capturing group and I used negation instead to eliminate the amount of backtracking.
someVar.match(/\[[^\]]*]/g);
In /\[(.*?)\]/, *? means lazy, matching as few content as possible.
What you actually want is all the matches in content. Try modifier g
Try this one, http://regex101.com/r/aD6cM8/1. Any match starts with [, ends with ], but doesn't allow[ or ] inbetween.
someVar.match(/\[([^\[\]]*)\]/g)
You should just add the g switch to your regex :
someVar.match(/\[(.*?)\]/); // result => ["[a=0]", "a=0"]
results in
[ "[a=0]", "[b-c=twelve]", "[def=one-2]" ]
Your regex is correct, just suffix g to it to make it global:
someVar.match(/\[(.*?)\]/g);
Here's more info on it: http://www.w3schools.com/jsref/jsref_regexp_g.asp

Replace all besides the Regex group?

I was given a task to do which requires a long time to do.
The image say it all :
This is what I have : (x100 times):
And I need to extract this value only
How can I capture the value ?
I have made it with this regex :
DbCommand.*?\("(.*?)"\);
As you can see it does work :
And after the replace function (replace to $1) I do get the pure value :
but the problem is that I need only the pure values and not the rest of the unmatched group :
Question : In other words :
How can I get the purified result like :
Eservices_Claims_Get_Pending_Claims_List
Eservices_Claims_Get_Pending_Claims_Step1
Here is my code at Online regexer
Is there any way of replacing "all besides the matched group" ?
p.s. I know there are other ways of doing it but I prefer a regex solution ( which will also help me to understand regex better)
Unfortunately, JavaScript doesn't understand lookbehind. If it did, you could change your regular expression to match .*? preceded (lookbehind) by DbCommand.*?\(" and followed (lookahead) by "\);.
With that solution denied, i believe the cleanest solution is to perform two matches:
// you probably want to build the regexps dynamically
var regexG = /DbCommand.*?\("(.*?)"\);/g;
var regex = /DbCommand.*?\("(.*?)"\);/;
var matches = str.match(regexG).map(function(item){ return item.match(regex)[1] });
console.log(matches);
// ["Eservices_Claims_Get_Pending_Claims_List", "Eservices_Claims_Get_Pending_Claims_Step1"]
DEMO: http://jsbin.com/aqaBobOP/2/edit
You should be able to do a global replace of:
public static DataTable.*?{.*?DbCommand.*?\("(.*?)"\);.*?}
All I've done is changed it to match the whole block including the function definition using a bunch of .*?s.
Note: Make sure your regex settings are such that the dot (.) matches all characters, including newlines.
In fact if you want to close up all whitespace, you can slap a \s* on the front and replace with $1\n:
\s*public static DataTable.*?{.*?DbCommand.*?\("(.*?)"\);.*?}
Using your test case: http://regexr.com?37ibi
You can use this (without the ignore case and multiline option, with a global search):
pattern: (?:[^D]+|\BD|D(?!bCommand ))+|DbCommand [^"]+"([^"]+)
replace: $1\n
Try simply replacing the whole document replacing using this expression:
^(?: |\t)*(?:(?!DbCommand).)*$
You will then only be left with the lines that begin with the string DbCommand
You can then remove the spaces in between by replacing:
\r?\n\s* with \n globally.
Here is an example of this working: http://regexr.com?37ic4

Categories

Resources