javascript - 查找两个字符串正则表达式中常见出现的单词

标签 javascript regex vb.net

我正在尝试做同样的事情:Find The Common Occurrences Of Words In Two Strings但我想要它在 VB.net 中。

var tester = "a string with a lot of words";

function getMeRepeatedWordsDetails ( sentence ) {
  sentence = sentence + " ";
  var regex = /[^\s]+/g;
  var regex2 = new RegExp ( "(" + tester.match ( regex ).join ( "|" ) + ")\\W", "g" );
  matches = sentence.match ( regex2 );
  var words = {};
  for ( var i = 0; i < matches.length; i++ ) {
    var match = matches [ i ].replace ( /\W/g, "" );
    var w = words [ match ];
    if ( ! w )
      words [ match ] = 1;
    else
      words [ match ]++;
  }   
  return words;
} 

console.log ( getMeRepeatedWordsDetails ( "another string with some words" ) );

这是我到目前为止所拥有的:

    Dim tester As String = "a string with a lot of words"
    sentence = sentence & " "
    Dim regex As New Regex("/[^\s]+/g")
    Dim m As Match = regex.Match(tester)

    'not working
    Dim regex2 As Regex = New Regex("(" + regex.Match(tester).join("|") + ")\\W", "g")

你能帮我把它放到 VB.net 中吗?

最佳答案

我想我已经明白了:

    'string of interest
    Dim sentence As String = "check this string with some words"
    sentence = sentence & " "

    'this is our base string
    Dim BaseStr As String = "base string with a lot of words"

    'use this one liner to create the pattern
    Dim pattern2 As String = "(" & BaseStr.Replace(" ", "|") & ")\W"

    'set up the regex
    Dim regex As New Regex(pattern2)
    Dim mc As MatchCollection
    mc = regex.Matches(sentence)

    'loop to get the results
    For ct As Integer = 0 To mc.Count - 1
        Dim m As Match = mc.Item(ct)
        If m.Success Then
            'returns two groups; get the second without a space
            Debug.Print(m.Groups(1).Captures(0).ToString() & "<<")
        End If
    Next

关于javascript - 查找两个字符串正则表达式中常见出现的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367155/

相关文章:

javascript - defer().promise 和 Promise 的区别

javascript - 如何检查 DOM 中是否存在元素

vb.net - 将值从 BackgroundWorker DoWork 传递到 BackgroundWorker Completed

asp.net - 在浏览器中调试/查看时为"503 Service Unavailable"

asp.net - VB.NET SQL 限制提交尝试次数

javascript - 将 teams-js 导入 Vue.js 文件

Javascript - 从下拉菜单中选择 - 运行一次

JavaScript regexp - 字符串中是否有字母

python,正则表达式拆分和特殊字符

javascript - jQuery 价格的正则表达式