matching a word from the textarea - javascript

I am trying to match words strating with # from a textarea
var start=/#/ig; // # Match
var word=/#(\w+)/ig; //#abc Match
var content=$('#text').val();
var go= content.match(start); //Content Matching #
var name= content.match(word); //Content Matching #abc
Here it checks the value in the text area and gives the output word which starts with #
If there is two matches found it's showing the two matches,but i need only the last match

the .match() function returns an array of matches.
If you want to select the last element, you need to select it like this:
name[name.length-1];
name.length returns the number of elements
-1 because array keys start with 0 and not 1
Here is a fiddle, thanks to Dave Briand

Try this
content = "~test content ~thanks ok ~fine";
strFine = content.lastIndexOf('~');
It gives index for last match character or word or string what you have passed...

The re method match usually returns an array. To get the last match, you can use the following:
var name= content.match(word);
var lastMatch = (!!name) ? name.pop() : name;
If there's no match, the method returns null and you want to be sure you do not try to manipulate null as an array.

What about this?
var text = $('textarea').val(); // "#maxi kcdjnd dcjjncd #ijicd"
//Find all ocurrences of words starting with # and ending in space, period or end of text
var lastMatch = text.match(/(#.*?)(\s|$|\.)/g).pop();
// match returns ["maxi", "#ijicd"]. pop will get the last element

Related

Calling characters from a string In JS

I am currently trying to grab a 9 character string from a title, the start of the list is always "BC-" and then it is always six digits following, so for instance a complete thing would look like - "BC-004352" my problem is that I can grab everything after the "BC-" however if there is something after that like "Words Words BC-004352 Words words" it then grabs the "BC-004352 Words Words". This will mess up my program, so is their any way of only capturing the "BC-004352"? How could I then make the script self executable as at the moment it is running of a button and that isn't helpful
<!--BC-Check six digit-->
<script type="text/javascript">
function bc_check() {
var str = "FUCKCKCKKC BC-040300 Has broken";
var res = str.substring(str.indexOf("BC-") + 0);
document.getElementById("recognize").innerHTML = res;
}
</script>
Or you can do this with Regular Expressions:
const testString = "FCKCKCKKC BC-040300 Has broken";
const regex = /.*?BC-(\d+).*?/; //Capture any number of digits following BC-
const matches = testString.match(regex); //Get the match collection
console.log(matches[1]); //Match collection index 1 holds your number
substring has a second parameter for indexEnd. It doesn't include the character at that index so you'll have to add one to get all of the chars you want. So in this case you'll want to add 10 to the index of "BC-".

How to get a sub-string after the last specified char ? Javascript

lets say I have the following string :
var text = "A_B_C_190"
I want to be able to extract the number at the end (the last 3 chars after the last _ )
I tired :
text.substr(text.indexOf('_'), -1)
but that gave me null
You can use string.prototype.split and array.prototype.pop:
var text = "A_B_C_190";
console.log(text.split('_').pop());
You have to use lastIndexOf method.
var text = "A_B_C_190"
ans = text.substr(text.lastIndexOf('_')+1)
console.log(ans)
If you want to convert it to an integer, use parseInt(ans)

Getting line number from index of character in file

I have a string input which consists of words. I am using regex.exec (g) to get all the words by function getWord(input)
So my input may look like this:
word word2
someword blah
What I get from from exec is object containing index of match. So it is array like:
[ 'word', index: 0, input: "..."]
...
[ 'someword', index: 11, input: "..."]
...
What I need is to easily calculate that word "someword" is on line 2 by using the index(11) (as I don't have any other value telling me what is the number of lines)
Here is what I came up with: Match '\n's until you match \n with higher index then is index of word. Not sure if this may not be problematic in 10k lines file.
Snippet for idea:
getLineFromIndex: (index, input) ->
regex = /\n/g
line = 1
loop
match = regex.exec(input)
break if not match? or match.index > index
line++
return line
Kinda big optimalization can be done here. I can save the regex and last match, so I won't iterate all the input every time I want to check for line number. Regex will then be executed only when the last match has lower index then current index.
This is the final idea with optimization:
###
#variable content [String] is input content
###
getLineFromIndex: (index) ->
#lineMatcher = #lineMatcher || /\n/g
#lastLine = #lastLine || 1
if #eof isnt true
#lastMatch = #lastMatch || #lineMatcher.exec(#content)
if #eof or index < #lastMatch.index
return #lastLine
else
match = #lineMatcher.exec(#content)
if not #eof and match is null
#eof = true
else
#lastMatch = match
#lastLine++
return #lastLine
Cut input (a.substr(0, 11)).
Split it (a.substr(0, 11).split('\n')).
Count it (a.substr(0, 11).split('\n').length).
Your pseudo-code seems to do the job.
But I do not see how you can infer the line number by the offset of the searched word.
I would split the input text by lines, then look over the array for the searched word, and if found return the line index.
var input= "word word2 \n"+
"someword blah";
function getLinesNumberOf( input, word){
var line_numbers=[];
input.split("\n").forEach(function(line, index){
if( line.indexOf(word)>=0 ) line_numbers.push(index);
});
return line_numbers;
}
console.log(getLinesNumberOf(input,"someword"));
I have add support for multiple occurences of the searched word.
edit
To avoid too memory consumption with large inputs, you can parse sequentially (for the same avantanges of SAX vs DOM):
function getLinesNumberOf( word, input ){
input+= "\n";//make sure to not miss the last line;
var line_numbers=[], current_line=0;
var startline_offset=0;
do{
//get the offset next of the next breakline
endline_offset= input.indexOf("\n",startline_offset);
//get the offset of the searched word in the line
word_offset= input.substring(startline_offset,endline_offset).indexOf(word, 0);
//check if the searched word has been found and if it has been found on current_line
if( word_offset >= 0 && word_offset < endline_offset ) {
//if true the current_line is stored
line_numbers.push(current_line);
}
//the offset of the next line is just after the breakline offset
startline_offset= endline_offset+1;
current_line++;
}while(endline_offset>=0);//we continue while a breakline is found
console.log(line_numbers);
}

REGEX: Finding the correct occurrence order of some given special characters in a string

I want a regular expression that would be able to find the correct occurrence order of
* | . | # | 'any HTML element type' like 'p' or 'div'
LIKE
var regex = //Some regular expression;
var pattern_1 = "*p.class1#id1.class2";
pattern_1.match(regex); // Should print ['*','p','.','#','.']
var pattern_2 = "p.class1#id1.class2";
pattern_2.match(regex); //Should print ['p','.','#','.']
var pattern_3 = "p#id1.class1.class2";
pattern_3.match(regex); //Should print ['p','#','.','.']
var pattern_4 = "#id.class1.class2";
pattern_4.match(regex); //should print ['#id','.','.']
var pattern_5 = "*#id.class1.class2";
pattern_5.match(regex); //should print ['*','#','.','.']
I am trying my luck with regex = /^\*?[a-zA-Z]*|\#|\./g but it doesn't work
You might be better off matching # and . along with their respective values and filtering the results afterwards:
var regex = /\*|([#.]\w+)|(\w+)/g
matches = pattern_1.match(regex).map(function(x) { return x.match(/^[#.]/) ? x.charAt(0) : x })
or remove the id/class names first, and then match:
matches = pattern_1.replace(/([#.])\w+/g, "$1").match(/[.#*]|\w+/g)
ok I have:
/(\*)|(\.)|([a-zA-Z]+)|(#)/g
the problem with this is that unless you specify all the possible html elements that you can capture like div, span etc then it will match all strings including class and id names.
Hope this helps anyway.
Edit live on Debuggex

trim RegExp result

I'm using RegExp to extract specific words from a string like the following:
<div class = ''></div>class class
My current RegExp statement is this:
/(<)[^(>)]*(;| |'|&quote;)(class)?( |'|&quote;|=)/gi
In this instance I wan t to match the word 'class' but only if it has specific characters (or ampersands) before and after it. This statement matches:
<div class
out of the original string, I'm using this in a replace method (to insert text where 'class' was) and I want to keep the other character around it, is there any way of trimming this match down to only select(replace) the word 'class' (in this instance)?
Or is there a better way of doing this?
Thanks in advance.
You can use next code to do this job:
var removeSubstring=(function(){
var fn=function($0,$1,$2,$3){return $1+$3;};
return function(str,before,after,removed){
var rg=new RegExp("("+before+")("+removed+")("+after+")","gi");
return str.replace(rg,fn);
};
})();
var str="<div class = ''></div>class class <div class ";
var before="<div ";
var after=" ";
var removed="class";
removeSubstring(str,before,after,removed);
// <div = ''></div>class class <div
But you must ensure what in before, after and removed strings does not contained special RegExp format characters (such as . [ ] ? * and others, see MDN RegExp). Otherwise you must first escape these special characters in before, after and removed strings.
UPDATE:
You also can use valid regular expression for before, after and removed to make more flexible search. For example next code remove letters 'X', 'V' and 'Z' but not remove 'x', 'v' and 'z':
var str="qXd ezrwsvrVa etxceZd";
var before="q|w|e|r";
var after="a|s|d|f";
var removed="z|x|c|v";
removeSubstring(str,before,after,removed);
// "qd ezrwsvra etxced"
If you need to replace substring with another string you can use next function:
function replaceSubstring(str,before,after,removed,inserted){
var rg=new RegExp("("+before+")("+removed+")("+after+")","gi");
return str.replace(rg,function($0,$1,$2,$3){
// $1 is searched "before" substring
// $2 is searched "removed" substring
// $3 is searched "after" substring
return $1+(inserted||"")+$3;
});
};
var str="qXd ezrwsvrVa etxceZd";
var before="q|w|e|r";
var after="a|s|d|f";
var removed="z|x|c|v";
replaceSubstring(str,before,after,removed,"DDDD")
// "qDDDDd ezrwsvrDDDDa etxceDDDDd"
Try this:
var match = /your regex here/.exec(myString)
match = match.replace('class','')

Categories

Resources